2

On the StackExchange-chat, I received quickly-made MWE's which answered parts of my question; yet remarkably show the experience and knowledge of the person who fabricated them.

For example, when I asked how to create a code which would automatically right-align a vertically stacked series of numbers (from 2 to 50), I got the following answers, which worked perfectly:

Answer 1:

\documentclass{article}

\newcounter{loopcounter}

\begin{document}
\setcounter{loopcounter}{50}
\loop\unless\ifnum\value{loopcounter}=1
\llap{\theloopcounter}
\addtocounter{loopcounter}{-1}
\repeat

\end{document}

Answer 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}

What I am wondering about, is how to regulate the vertical spacing between the different lines of numbers? In other words: the line-spacing.

1

A recursive stack (\fbox thrown in for flair [easily removed]). Note also the stack spacing has no effect on the underlying line spacing of the 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}{\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}

enter image description here

If one wants the spacing to be the inter-baselineskip, rather than the inter-character vertical gap, then this:

\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}

enter image description here

| improve this answer | |
  • I don't have enough experience to know whether your answer or your colleague's answer is more brilliant. But thank you anyway: it's certainly brilliant! – Vincent Mia Edie Verheyen Jul 8 '16 at 20:35
2

I can suggest a key-value interface:

\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}

enter image description here

| improve this answer | |
  • I don't have enough experience to know whether your answer or your colleague's answer is more brilliant. But thank you anyway: it's certainly brilliant! – Vincent Mia Edie Verheyen Jul 8 '16 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.