2

Here is the MWE:

\documentclass{article}
\usepackage{pgffor, geometry, tabu, booktabs}
\geometry{
  a4paper, 
  portrait, 
  margin=1in
}
\begin{document}

Here is creating the table using a loop\\[10pt]

    \foreach \n in {Feb, Mar, April is the best month}{
      \begin{tabu}{X[-1] X}
        \n & \foreach \m in {0,...,30}{\framebox(9,9){} }\\ \midrule
      \end{tabu}
}
\vskip 20pt
Here is creating table one row at a time\\[10pt]

      \begin{tabu}{X[-1] X}
        Feb & \foreach \n in {0,...,30}{\framebox(9,9){} }\\ \midrule
        Mar & \foreach \n in {0,...,30}{\framebox(9,9){} }\\ \midrule
        April is the best month & \foreach \n in {0,...,30}{\framebox(9,9){} }\\ \midrule
      \end{tabu}
\end{document}

Table in the loop is broken --- no alignment of columns.

How do I get the table in the loop set as the table without the loop?

5
  • 2
    why would you expect alignment in the first case? You have not made a single table but three tables each with X columns so each independently chooses a column width. Feb 18 '17 at 10:25
  • See tex.stackexchange.com/q/309765/15925 and note that the inner loop variable should be given a new name. Feb 18 '17 at 10:32
  • @DavidCarlisle My bad. I agree. But when I moved \begin{tabu} before the foreach for months, and \end{tabu} after closing the foreach for months, it produced an extra } or forgotten \egroup error.
    – deshmukh
    Feb 18 '17 at 10:38
  • The linked question shows you how to put the table contents in to a macro first. Feb 18 '17 at 10:40
  • @AndrewSwann I tried basing my case on the structure provided in the link. But could not. And after reading your answer, I realize I could not have, either. There are two main differences, & after \n and tablecontents within the macro.
    – deshmukh
    Feb 18 '17 at 10:50
2

This is a duplicate of Foreach loop in tabular, missing endgroup inserted, but here is the code for this particular case:

\documentclass{article}

\usepackage{pgffor, geometry, tabu, booktabs, etoolbox}

\geometry{
  a4paper,
  portrait,
  margin=1in
}

\begin{document}

\newcommand{\tablecontents}{}
\foreach \n in {Feb, Mar, April is the best month} {
\xappto\tablecontents{\n &}
\foreach \m in {0,...,30}{\gappto\tablecontents{\framebox(9,9){} } }
\gappto\tablecontents{\\ \midrule}
}

Here is creating the table using a loop\\[10pt]

\begin{tabu}{X[-1] X}
\tablecontents
\end{tabu}

\end{document}

Sample output

To put the date \m in each box you need to be careful about expansion, so the inner loop line becomes

\foreach \m in {0,...,30}{\xappto\tablecontents{\noexpand\framebox(9,9){\m} } }

\xappto expands it contents, which you need to get the value of \m, but expansion of \framebox needs to be done later. \gappto does no expansion.

2
  • When I try to put the date in the box \framebox(9,9){\m}, it produces an undefined control sequence error. Will you please point me to the documentation that explains this? I will give it a try and see if I can come up with a solution.
    – deshmukh
    Feb 18 '17 at 10:57
  • 1
    Code now added for this. Feb 18 '17 at 11:02
1

You have to build the table body before doing the table. Here's an implementation with xparse.

\documentclass{article}
\usepackage{geometry, tabularx, booktabs}
\usepackage{xparse}
\geometry{
  a4paper, 
  portrait, 
  margin=1in
}

\ExplSyntaxOn
\seq_new:N \l__deshmukh_monthtable_in_seq
\seq_new:N \l__deshmukh_monthtable_out_seq

\cs_new_protected:Nn \__deshmukh_printmonth:n
 {
  { #1 } & \prg_replicate:nn { 30 } { \framebox(9,9){} ~ }
 }

\NewDocumentCommand{\monthtable}{m}
 {
  \seq_set_split:Nnn \l__deshmukh_monthtable_in_seq {,} { #1 }
  \seq_clear:N \l__deshmukh_monthtable_out_seq
  \seq_map_inline:Nn \l__deshmukh_monthtable_in_seq
   {
    \seq_put_right:Nn \l__deshmukh_monthtable_out_seq
     { \__deshmukh_printmonth:n { ##1 } }
   }
  \noindent
  \begin{tabularx}{\textwidth}{l>{\raggedright\arraybackslash}X}
  \toprule
  \seq_use:Nn \l__deshmukh_monthtable_out_seq { \\ \midrule }
  \\ \bottomrule
  \end{tabularx}
 }
\ExplSyntaxOff

\begin{document}

\monthtable{Feb,Mar,April is the best month}

\end{document}

enter image description here

1
  • I thought the OP wanted the month titles and 31 boxes to form 32 aligned columns Feb 18 '17 at 10:36

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.