1

I'm redefining description to use a table, as part of a CV class. The CV is styled as a big long list, with vertically aligned dates and such, so I thought I could add \begin{tabular}{ l l } to AtBeginDocument, and end it at AtEndDocument. Then just redefine description to be transparent, and items within description to be the rows of the table as expected:

\renewenvironment{description}
   {
     \renewcommand{\item}[1][]{\textsc{##1} & }
   } {
   }

As inspired by this blog post.

By defining things like section simlarly, I can add a section to my CV with:

\section{A section}
\begin{description}
\item[2019] An event \\
\item[2021] A later event \\
\end{description}

I've been testing my ideas in smaller files as I go along. So the total MWE, exactly as I compiled it:

\documentclass{article}

\renewenvironment{description}
   {
      \renewcommand{\item}[1][]{##1 & }
   } {
   }

\begin{document}
\begin{tabular}{ l l }   
\begin{description}

\item[2019] An event \\
\item[2021] A later event \\
          
\end{description}
\end{tabular}

\end{document}

This compiles, but it's wrong. It looks like this:

error

To get the whole lot to show up, I have to include the tabular environment in the definition of description:

\documentclass{article}

\renewenvironment{description}
   {
     \renewcommand{\item}[1][]{##1 & }
     \begin{tabular}{ l l }   
   } {
     \end{tabular}
   }

\begin{document}
\begin{description}

\item[2019] An event \\
\item[2021] A later event \\
          
\end{description}

\end{document}

Which looks as expected, like this:

enter image description here

This happens with more than two items as well. Only the top item ever shows up completely, the others are truncated as in the image.

I know I could just keep the tabular environment in the description definition, but that seems untidy, and would be harder to maintain (because I would need a similar declaration in every environement and/or command I redefined, to make everything line up properly. I originally had all this hardcoded in one LaTeX file, and there I used a single table wrapping the whole file, and was much easier than having lots of individual ones).

So, why does this happen, and how can I make the output appear as expected, with only one table, outside of the environment definition?

2 Answers 2

1

With the description environment within the tabular, your redefinition of \item is limited to the scope of the first cell within the tabular. Beyond that, \item reverts back to its original definition and things fail. You'll have to make the assignment (\renewcommand{\item}) global for it to extend beyond the cell where it's redefined in.

Another problem with the current setup is that the cells (effectively groups) within the tabular now has a description environment (another group) starting in the first cell and closing in another cell. The group has to be opened/closed within the same group. One way around this is to capture the entire contents of this newly-defined description environment and only set at \end{description}. This is made possible via environ.

Here's a complete minimal example that captures the above ideas:

enter image description here

\documentclass{article}

\usepackage{environ}

\newcommand{\itemwithoptarg}[1][]{#1 &}
\let\olditem\item% Capture current definition of \item
\newcommand{\restoreitem}{\global\let\item\olditem}
\RenewEnviron{description}{%
  \gdef\item{\itemwithoptarg}% Global redefinition of \item
  \global\let\BODY\BODY% Make \BODY global
  \aftergroup\BODY% Print \BODY after description environment closes
  \aftergroup\restoreitem% Restore original \item (if you use lists, for example)
}

\begin{document}

\begin{tabular}{ l l }   
  \begin{description}
    \item[2019] An event \\
    \item[2021] A later event \\
  \end{description}
\end{tabular}

\begin{itemize}
  \item First
  \item Second
\end{itemize}

\end{document}
3

after any error don't even look at the PDF except as a possible debugging aid, Tex makes no attempt at sensible output if you choose to scroll past an error, it just recovers enough to try to syntax check the rest of the document.

Your code produces

! Extra }, or forgotten \endgroup.

as you have \begin{description} in the first cell of the table, nut a table is a group so as soon as it sees & the group ends but the environment hasn't finished.

basically a & is like }{ it ends one group and starts another and you would get essentially the same error from

 {  \begin{description}  }{ \end{description} }

I would say putting the tabular in to the definition would be the right markup here. (Personally I'd use new names not redefine description but that's just a matter of personal preference)

that said you could keep your definition if you invert the calling order:

\documentclass{article}

\renewenvironment{description}
   {
      \renewcommand{\item}[1][]{##1 & }
   } {
   }

\begin{document}
\begin{description}
\begin{tabular}{ l l }   

\item[2019] An event \\
\item[2021] A later event \\
\end{tabular}
          
\end{description}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .