Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

It's been a while since I asked an embarrassing question, so here is one for your amusement: I want to print a table of numbers that is able to adapt to various column sizes so can't use a tabular like environment.

Well, I thought that this would be straight forward and a simple solution such as to use a \makebox[3em][r]{} should work. And of course, I need a space on one side so that it could break at line boundaries.

And it fact it works pretty good, but not exactly right. So what I have below only seems to only have a problem with the last row as I get the following result with the MWE below:

enter image description here

Code:

\documentclass{article}
\usepackage{showframe}
\usepackage{pgffor}

\newcommand*{\ListOrPrimes}{%
      2,      3,      5,      7,     11,     13,     17,     19,     23,     29, 
     31,     37,     41,     43,     47,     53,     59,     61,     67,     71, 
     73,     79,     83,     89,     97,    101,    103,    107,    109,    113, 
    127,    131,    137,    139,    149,    151,    157,    163,    167,    173,
    179,    181,    191,    193,    197,    199,    211,    223,    227,    229
}%

\begin{document}

\noindent
The sequence of primes is:
\medskip
{\raggedright%
\par\noindent\foreach \x in \ListOrPrimes{%
        \makebox[4em][r]{\x}\space%
}%

\end{document}
share|improve this question
    
You're missing an end-of-line % after 229. –  Werner Jan 28 '13 at 7:31
    
Well of course only after actually posting a question does a solution present it self... :-). I'll leave it up in case someone want to figure it out. Yeah, what Werner said!! But why? It didn't seem to create any problems on the other entries. –  Peter Grill Jan 28 '13 at 7:31
    
The other entries all have a preceding space, while 229 comes with a trailing space. And, since you're using a right-aligned \makebox, only the trailing space of an entry is revealed this way. –  Werner Jan 28 '13 at 7:33
    
Hmmm, actually although that fixes it with the MWE in my actual use case the two entries before the last one exhibit the problem -- the last one aligns just fine... –  Peter Grill Jan 28 '13 at 7:34
    
How about \makebox[4em][r]{\x\unskip} for the definition inside \pgffor? –  Werner Jan 28 '13 at 7:36

1 Answer 1

up vote 6 down vote accepted

If you use LaTeX3 facilities, the problem of leading and trailing spaces will disappear:

\documentclass{article}
\usepackage[pass,showframe]{geometry}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\printlist}{s O{4em} m}
 {
  \par\noindent
  \IfBooleanTF#1
   { \clist_map_inline:Vn #3 { \grill_print_item:nn { #2 } { ##1 } } }
   { \clist_map_inline:nn { #3 } { \grill_print_item:nn { #2 } { ##1 } } }
  \par
 }
\cs_generate_variant:Nn \clist_map_inline:nn { V }
\cs_new:Npn \grill_print_item:nn #1 #2
 {
  \makebox[#1][r]{#2}~
 }
\ExplSyntaxOff

\newcommand*{\ListOfPrimes}{
      2,      3,      5,      7,     11,     13,     17,     19,     23,     29, 
     31,     37,     41,     43,     47,     53,     59,     61,     67,     71, 
     73,     79,     83,     89,     97,    101,    103,    107,    109,    113, 
    127,    131,    137,    139,    149,    151,    157,    163,    167,    173,
    179,    181,    191,    193,    197,    199,    211,    223,    227,    229
}

\begin{document}
\noindent This is a list of primes:
\printlist*{\ListOfPrimes}

\medskip

\noindent This is another:
\printlist[2em]{2,3, 5, 7}
\end{document}

enter image description here

With \printlist* you give as argument a control sequence (expanding to comma separated list); without the * you simply give the list. The optional argument is the width of the boxes.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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