在 StackExchange 聊天中,我收到了快速制作的 MWE,它回答了我的部分问题;但同时也显著地展示了制作它们的人的经验和知识。
例如,当我询问如何创建一个代码,使垂直堆叠的一系列数字(从 2 到 50)自动右对齐时,我得到了以下答案,它们非常完美:
答案 1:
\documentclass{article}
\newcounter{loopcounter}
\begin{document}
\setcounter{loopcounter}{50}
\loop\unless\ifnum\value{loopcounter}=1
\llap{\theloopcounter}
\addtocounter{loopcounter}{-1}
\repeat
\end{document}
答案 2:
\documentclass{article}
\newcounter{loopcounter}
\makeatletter
\newcommand{\loopdown}[2]{%
\c@loopcounter=#1%
\loop\unless\ifnum\value{loopcounter}=#2
\llap{\theloopcounter}%
\advance \c@loopcounter by -1
\repeat
}
\makeatother
\begin{document}
\loopdown{50}{1}
\end{document}
我所疑惑的是,如何调节不同行数字之间的垂直间距?换句话说:行距。
答案1
递归堆栈(\fbox
为了增加光彩而加入 [容易移除])。还请注意,堆栈间距对文档的底层行距没有影响。
\documentclass{article}
\usepackage{stackengine}
\newcounter{loopcounter}
\newcommand\makestack[3]{%
\def\currentstack{#2}%
\setcounter{loopcounter}{\the\numexpr#2-1\relax}%
\loop\unless\ifnum\value{loopcounter}=\numexpr#3-1\relax%
\savestack{\currentstack}{\stackengine{#1}{\currentstack}{\theloopcounter}%
{U}{r}{F}{F}{S}}%
\addtocounter{loopcounter}{-1}%
\repeat%
\fbox{\currentstack}%
}
\begin{document}
\makestack{2pt}{50}{2}%
x%
\makestack{1pt}{50}{2}%
x%
\makestack{.5pt}{50}{2}%
x%
\makestack{.5pt}{47}{11}%
\end{document}
如果希望间距为基线间间隙,而不是字符间垂直间隙,则如下:
\documentclass{article}
\usepackage{stackengine}
\newcounter{loopcounter}
\newcommand\makestack[3]{%
\def\currentstack{#2}%
\setcounter{loopcounter}{\the\numexpr#2-1\relax}%
\loop\unless\ifnum\value{loopcounter}=\numexpr#3-1\relax%
\savestack{\currentstack}{\stackengine{#1}{\theloopcounter}{\currentstack}%
{O}{r}{F}{F}{L}}%
\addtocounter{loopcounter}{-1}%
\repeat%
\fbox{\currentstack}%
}
\begin{document}
\makestack{9pt}{50}{2}%
x%
\makestack{8.5pt}{50}{2}%
x%
\makestack{8pt}{50}{2}%
x%
\makestack{8pt}{47}{11}%
\end{document}
答案2
我可以建议一个键值接口:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\keys_define:nn { vincent/printnumbers }
{
start .int_set:N = \l_vincent_printnumbers_start_int,
step .int_set:N = \l_vincent_printnumbers_step_int,
end .int_set:N = \l_vincent_printnumbers_end_int,
wrapper .tl_set:N = \l_vincent_printnumbers_wrapper_tl,
sep .tl_set:N = \l_vincent_printnumbers_sep_tl,
start .value_required:n = true,
step .value_required:n = true,
end .value_required:n = true,
wrapper .initial:n = \use:n,
sep .initial:n = {,~},
}
\NewDocumentCommand{\printnumbers}{m}
{
\group_begin:
\keys_set:nn { vincent/printnumbers } { #1 }
\seq_clear:N \l_vincent_printnumbers_seq
\int_step_inline:nnnn
{ \l_vincent_printnumbers_start_int }
{ \l_vincent_printnumbers_step_int }
{ \l_vincent_printnumbers_end_int }
{
\seq_put_right:Nx \l_vincent_printnumbers_seq
{
\exp_not:V \l_vincent_printnumbers_wrapper_tl { ##1 }
}
}
\seq_use:Nn \l_vincent_printnumbers_seq { \l_vincent_printnumbers_sep_tl }
\group_end:
}
\ExplSyntaxOff
\begin{document}
\noindent\printnumbers{
start=20,
end=2,
step=-1,
wrapper=\makebox[1em][r],
sep=\\[10pt],
}
\noindent\printnumbers{
start=1,end=30,step=4,
}
\end{document}