[Tex/LaTex] How to typeset LaTeX code inside a Table environment

best practicescodeverbatim

I wanted to typeset LaTeX code directly into my document, but without it doing something, i.e., I just want to show my code in the output.

Now, I know there is the verbatim environment for code, as well as the listings package for this, but I am wondering what the best method for typesetting LaTeX code directly is (if there is one), without needing any extra packages.

A few days ago I tried to do it inside a table, and I came up with this question: Print small TeX code verbatim and render it

but still, they managed to do something different, as it automatically included the showcase.

I just want to be able to write a LaTeX code snippet easily and inside a normal environment, say, tables. What is the best way to do it?

MWE

\documentclass{report}

\usepackage{booktabs}

\begin{document}

\begin{table}[tbhp]
\centering
\caption{Citing with FEUPPHDTESES.STY}\label{tab:Citing}
\begin{tabular}{ll}
\toprule
Command & Result\\
\midrule
\texttt{citeplist}  & \verb\citeplist{peel_epidemiology_2011,espinoza2012inverse,espinoza2012optimization}\\
\texttt{citet}          & \verb\citet{peel_epidemiology_2011,espinoza2012inverse,espinoza2012optimization}\\
\texttt{citep}          & \verb\citep{peel_epidemiology_2011,espinoza2012inverse,espinoza2012optimization}\\
\texttt{citeyear}       & \verb\citeyear{peel_epidemiology_2011}\\
\texttt{citeauthor} & \verb\citeauthor{peel_epidemiology_2011}\\
\texttt{citetlist}  & \verb\citetlist{peel_epidemiology_2011,espinoza2012inverse,espinoza2012optimization}\\
\texttt{citeplist}  & \verb\citeplist{peel_epidemiology_2011,espinoza2012inverse,espinoza2012optimization}\\
\bottomrule
\end{tabular}
\end{table}

\end{document}

Best Answer

The syntax for \verb is

\verb<char><text><char>

where <char> should be a (non special) character not found in <text>. Most often | or + are used for <char>.

A verbatim environment doesn't make sense in a table cell belonging to a column declared as l, c or r, just like a quote environment doesn't make sense in the argument to \mbox.

If you have multiline verbatim to be used in a cell column, you need to use a p column, but with some adjustments.

\documentclass{report}

\usepackage{booktabs,array}

\makeatletter
\newcolumntype{V}[1]{>{\topsep=0pt\@minipagetrue}p{#1}<{\vspace{-\baselineskip}}}
\makeatother
\newcommand{\command}[1]{\texttt{\string#1}}


\setlength{\parindent}{0pt} % just for the example

\begin{document}

\begin{tabular}{ll}
\toprule
Command & Example\
\midrule
\command{\citeplist}
 & \verb|\citeplist{peel_epidemiology_2011,espinoza2012inverse}|
 \\
\command{\citet}
 & \verb|\citet{peel_epidemiology_2011,espinoza2012inverse}|
 \\
\command{\citep}
 & \verb|\citep{peel_epidemiology_2011,espinoza2012inverse}|
 \\
\command{\citeyear}
 & \verb|\citeyear{peel_epidemiology_2011}|
 \\
\command{\citeauthor}
 & \verb|\citeauthor{peel_epidemiology_2011}|
 \\
\command{\citetlist}
 & \verb|\citetlist{peel_epidemiology_2011,espinoza2012inverse}|
 \\
\command{\citeplist}
 & \verb|\citeplist{peel_epidemiology_2011,espinoza2012inverse}|
 \\
\bottomrule
\end{tabular}

\bigskip

\begin{tabular}{lV{280pt}}
\toprule
Command & Example\\
\midrule
\command{\citeplist} &
\begin{verbatim}
\citeplist{peel_epidemiology_2011,espinoza2012inverse,
           espinoza2012optimization
\end{verbatim}
\\
\command{\citet} &
\begin{verbatim}
\citet{peel_epidemiology_2011,espinoza2012inverse,
       espinoza2012optimization
\end{verbatim}
\\
\command{\citeyear} &
\begin{verbatim}
\citeyear{peel_epidemiology_2011}
\end{verbatim}
\\
\bottomrule
\end{tabular}


\end{document}

enter image description here