TeX - LaTeX Asked by Frosten on June 11, 2021
I’m trying to reproduce a format of a list in latex and I can’t find how to always have the same amount of space before each arrow even if there is some text. How to get automatically the same amount of space before "-> <Assign>" and before "-> <If>"?
You have several options. I use the following helper macro throughout this:
newcommandmeta[1]{$langle$#1$rangle$}
We have to put relax
after if the following line starts with
[
, or else this is interpreted as the optional argument to .
tabular
Perhaps the easiest.
begin{tabular}{ll@{ $rightarrow$ }l}
[4] & meta{Instruction} & meta{Assign} relax
[5] & & meta{If} relax
[6] & & meta{While} relax
[7] & & meta{Print} relax
[8] & & meta{Read} relax
[9] & meta{Assign} & [VarName] := meta{ExprArith}
end{tabular}
tabbing
Just because this is seldomly used.
begin{tabbing}
[4] = meta{Instruction} = $rightarrow$ meta{Assign} relax
[5] > > $rightarrow$ meta{If} relax
[6] > > $rightarrow$ meta{While} relax
[7] > > $rightarrow$ meta{Print} relax
[8] > > $rightarrow$ meta{Read} relax
[9] > meta{Assign} > $rightarrow$ [VarName] := meta{ExprArith}
end{tabbing}
Complete example showing tabular
and tabbing
:
documentclass[]{article}
usepackage{array}
newcommandmeta[1]{$langle$#1$rangle$}
begin{document}
begin{tabular}{ll@{ $rightarrow$ }l}
[4] & meta{Instruction} & meta{Assign} relax
[5] & & meta{If} relax
[6] & & meta{While} relax
[7] & & meta{Print} relax
[8] & & meta{Read} relax
[9] & meta{Assign} & [VarName] := meta{ExprArith}
end{tabular}
begin{tabbing}
[4] = meta{Instruction} = $rightarrow$ meta{Assign} relax
[5] > > $rightarrow$ meta{If} relax
[6] > > $rightarrow$ meta{While} relax
[7] > > $rightarrow$ meta{Print} relax
[8] > > $rightarrow$ meta{Read} relax
[9] > meta{Assign} > $rightarrow$ [VarName] := meta{ExprArith}
end{tabbing}
end{document}
Result:
Instead of tabular
you could use longtable
to get something that is page breakable.
description
environment with enumitem
This one is admittedly a bit of a hack, but one could also define a formatting macro and use a description
list. The following defines myformatter
to format the start of each row. It takes two optional arguments, the first should be the widest entry of the second column, in this case Instruction
, the second should be the widest number in the line numbering.
documentclass[]{article}
newcommandmeta[1]{$langle$#1$rangle$}
newcounter{mylinenumber}
makeatletter
NewDocumentCommandmyformatter { O{} O{0} m }
{%
[phantom{#2}llap{arabic{mylinenumber}}]% right aligned, brackets outside
%[rlap{arabic{mylinenumber}}phantom{#2}]% left aligned, brackets outside
%phantom{[#2]}llap{[arabic{mylinenumber}]}% right aligned, brackets packed
%rlap{[arabic{mylinenumber}]}phantom{[#2]}% left aligned, brackets packed
hspace{1em}%
ifx@itemlabel#3%
else
rlap{meta{#3}}%
fi
phantom{meta{#1}}%
hspace{.5em}$rightarrow$hspace{.5em}%
stepcounter{mylinenumber}%
ignorespaces
}
makeatother
usepackage{enumitem}
begin{document}
setcounter{mylinenumber}{6}% number of the first row
begin{description}[{format=normalfontmyformatter[Instruction][10]}]
item[Instruction] meta{Assign}
item meta{If}
item meta{While}
item meta{Print}
item meta{Read}
item[Assign] [VarName] := meta{ExprArith}
end{description}
end{document}
Result:
There are different ways how you could align the brackets and line numbers, the one shown above is the one not commented out. Pick the version you like best by commenting out the others while keeping the one alignment you like (the comments after each line should hint at what this looks like).
tabular
approach.documentclass[]{article}
usepackage{array}
newcommandmeta[1]{$langle$#1$rangle$}
newcounter{mylinenumber}
newcommand*mylinenumber[1][0]
{%
[phantom{#1}llap{arabic{mylinenumber}}]%
stepcounter{mylinenumber}%
}
begin{document}
setcounter{mylinenumber}{4}% number of the first row
begin{tabular}{>{mylinenumberhspace{1em}}l@{ $rightarrow$ }l}
meta{Instruction} & meta{Assign} relax
& meta{If} relax
& meta{While} relax
& meta{Print} relax
& meta{Read} relax
meta{Assign} & [VarName] := meta{ExprArith}
end{tabular}
end{document}
Answered by Skillmon on June 11, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP