I know that \\
is a very overloaded command in LaTeX, but what exactly does it do in the tabular
environment?
Following my previous question, I have redefined the description
environment, effectively as a shorthand for tabular cells:
\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}{ p{6em} l }
\begin{description}
\item[2019] An entry \\
\item[2021] A later entry \\
\end{description}
\end{tabular}
\end{document}
I use this a lot in the document I'm working on, with short entries, so I thought it might be handy to code away the need for \\
at the end of each line. That shouldn't be too hard right?
Unfortunately I can't find the exact question where I found a solution very similar to this, but it looked like this:
\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}
\let\par=\cr
\obeylines
\begin{tabular}{ p{6em} l }
\begin{description}
\item[2019] An entry
\item[2021] A later entry
\end{description}
\end{tabular}
\end{document}
Since \obeylines
just appends \par
to every line, this just redefines \par
to be a separator for tables, then invokes \obeylines
.
But this doesn't work -- the document won't compile. I then tested this by going back to the first MWE and replacing every \\
with \cr
. This failed with similar errors. I don't know enough about LaTeX to be sure, but I imagine \obeylines
is working fine, and the problem is that I haven't redefined \par
correctly.
So, what does \\
do in the tabular environment (to which I should redefine \par
)? How could I have found this? I tried both \meaning
and \show
but they both gave me errors, and I don't know much about the internals of LaTeX.
enumitem
-based approach, such as the following?\documentclass{article} \usepackage{enumitem} \begin{document} \begin{description}[leftmargin=6em,style=nextline, font=\normalfont] \item[2019] An entry \item[2021] A later entry \item[long text] Another entry with a very long text that takes up more than one line. \end{description} \end{document}
.\item[xyz]
as a shorthand forxyz &
is not actually shorter?