One can use SAGE to get a LaTeX output for posets. For example entering



in the sage cell (https://sagecell.sagemath.org/) will display the poset u and then give a TikZ output to obtain it in LaTeX.

Now in SAGE the points of a poset with n points are enumerated from 0 to n-1 instead of 1 to n.

Question: Is there an easy way to get the LaTeX output where the points are enumerated by 1 to n (by setting i->i+1 in the SAGE graphic)?

Of course one can do this by hand for a given example, but for big posets it would be better to have an automatic way of doing this (via SAGE?).

2 Answers 2


Using this page on poset documentation, you just need to use relabel() to change the vertices. SAGE takes care of the rest.

output = latex(u)

Q = u.relabel(lambda x: x+1)
output2 = latex(Q)
The original poset in SAGE is this:


After relabelling the vertices, it now looks like this:


The output running in Cocalc is: enter image description here


I assume you compile the code output by latex(u) after doing u = posets.BooleanLattice(4) in Sage. In this case, you can use expsagetex to retrieve this LaTeX (actually, TikZ) code and store it in a macro (here: a tl variable). Then, it's a bit of a hack, but you can use l3regex to (sort of) replace every occurrence of L=\hbox{$〈number〉$} with L=\mbox{$〈number+1〉$} in the retrieved code:

% document.tex

\tl_new:N \l__mare_diagram_code_tl
\tl_new:N \l__mare_diagram_ID_tl

\NewDocumentCommand \myInsertDiagram { m }
    \est_record_formatted:Nn \l__mare_diagram_ID_tl {#1}
    \est_refused:V \l__mare_diagram_ID_tl
    \tl_set:Nx \l__mare_diagram_code_tl
      { \est_get:Vnn \l__mare_diagram_ID_tl {??} { \emph{paused} } }
    \regex_replace_all:nnN { L\ *\=\ *\c{hbox} \cB\{ \cM\$ (\d+) \cM\$ \cE\} }
      { L\=\c{mbox} \cB\{ \cM\$ \c{int_eval:n} \cB\{ \1 + 1 \cE\} \cM\$ \cE\} }
    \tl_use:N \l__mare_diagram_code_tl


% The default output at scale=1 has many overlapping nodes
\tikzset{every picture/.append style={scale=2}}


Run for instance pdflatex document.tex, then sage document.sagetex.sage, then again pdflatex document.tex and you'll obtain:

enter image description here

  • This isn't really a solution yet. In poset, the positioning of the vertices is important, as this Wikipedia entry explains. In the diagram you'll see: "Distinct sets on the same horizontal level are incomparable with each other."
    – DJP
    Commented Feb 18, 2020 at 16:37
  • @DJP Well, this layout is not the responsibility of my code. It is how tkz-graph formats the result of latex() called on the posets.BooleanLattice(4) object. I'm afraid that is where you should direct your complaints...
    – frougon
    Commented Feb 18, 2020 at 16:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .