Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Fix latex and unicode/ascii art
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorkarn committed Oct 11, 2022
1 parent a4a9474 commit 740651f
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/sage/combinat/diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ def _ascii_art_(self):
. . . . . .
. . . . . .
"""
from sage.typeset.ascii_art import ascii_art
return ascii_art(self._pretty_print())

def _unicode_art_(self):
Expand All @@ -249,6 +250,7 @@ def _unicode_art_(self):
☐☐☐☐☐☐
☐☐☐☐☐☐
"""
from sage.typeset.unicode_art import unicode_art
return unicode_art(self._pretty_print('☒', '☐'))

def _pretty_print(self, cell='O ', empty='. '):
Expand All @@ -263,16 +265,9 @@ def _pretty_print(self, cell='O ', empty='. '):
sage: from sage.combinat.diagram import Diagram
sage: Diagram([(0,0), (0,3), (2,2), (2,4)])._pretty_print('x ','. ')
x . . x .
. . . . .
. . x . x
'x . . x . \n. . . . . \n. . x . x \n'
sage: Diagram([(0,0), (0,3), (2,2), (2,4)], n_rows=6, n_cols=6)._pretty_print('x ','. ')
x . . x . .
. . . . . .
. . x . x .
. . . . . .
. . . . . .
. . . . . .
'x . . x . . \n. . . . . . \n. . x . x . \n. . . . . . \n. . . . . . \n. . . . . . \n'
"""
output_str = ''

Expand All @@ -292,38 +287,46 @@ def _latex_(self):
sage: from sage.combinat.diagram import Diagram
sage: latex(Diagram([]))
{\emptyset}
sage: latex(Diagram([(0,0), (0,3), (2,2), (2,4)]))
{\def\lr#1{\multicolumn{1}{|@{\hspace{.6ex}}c@{\hspace{.6ex}}|}{\raisebox{-.3ex}{$#1$}}}
\raisebox{-.6ex}{$\begin{array}[b]{*{5}{p{0.6ex}}}\cline{1-1}\cline{4-4}
\lr{\phantom{x}}&&&\lr{\phantom{x}}&\\\cline{1-1}\cline{4-4}
&&&&\\\cline{3-3}\cline{5-5}
&&\lr{\phantom{x}}&&\lr{\phantom{x}}\\\cline{3-3}\cline{5-5}
\end{array}$}
}
"""
if not self.cells():
return "{\\emptyset}"
from string import Template
lr = Template(r'\def\lr#1{\multicolumn{1}{$|@{\hspace{.6ex}}c@{\hspace{.6ex}}$|}{\raisebox{-.3ex}{$$#1$$}}}')

lr = r'\def\lr#1{\multicolumn{1}{|@{\hspace{.6ex}}c@{\hspace{.6ex}}|}{\raisebox{-.3ex}{$#1$}}}'

array = []
for i in range(self._n_rows):
row = []
for j in range(self._n_cols):
row.append("\\phantom{x}" if (i, j) in self else None)
array.append(row)

def end_line(r):
# give the line ending to row ``r``
if r == 0:
return "".join(r'\cline{%s-%s}'%(i+1, i+1) for i,j in enumerate(array[0]) if j != None)
elif r == len(array):
return "".join(r'\cline{%s-%s}'%(i+1, i+1) for i,j in enumerate(array[r-1]) if j != None)
return r"\\" + "".join(r'\cline{%s-%s}'%(i+1, i+1) for i,j in enumerate(array[r-1]) if j != None)
else:
out = "".join(r'\cline{%s-%s}'%(i+1, i+1) for i,j in enumerate(array[r-1]) if j != None)
out = r"\\" + "".join(r'\cline{%s-%s}'%(i+1, i+1) for i,j in enumerate(array[r-1]) if j != None)
out += "".join(r'\cline{%s-%s}'%(i+1, i+1) for i,j in enumerate(array[r]) if j != None)
return out

# now we draw the arrayarray
tex=r'\raisebox{-.6ex}{$\begin{array}[b]{*{%s}{p{0.6ex}}}'%(max(map(len,array)))
tex+=end_line(0)+'\n'
for r in range(len(array)):
tex+='&'.join('' if c is None else r'\lr{%s}'%(c,) for c in array[r])
tex += end_line(r+1)+'\n'
return tex+r'\end{array}$}'
return '{%s\n%s\n}' % (lr, tex+r'\end{array}$}')

def number_of_rows(self):
r"""
Expand Down

0 comments on commit 740651f

Please sign in to comment.