17

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}
12
  • 1
    What about the standard \verb command and the verbatim environment? They are available without extra packages... of course they have no syntax highlighting... Commented Oct 25, 2013 at 13:11
  • 1
    @SeanAllred yeap, I also got to that question. But mine still remains, if \verb cannot be used and neither begin{verbatim}... how can I use it? Most importantly, how did the guys that wrote The LaTeX Companion did it? Commented Oct 25, 2013 at 13:30
  • 2
    If you use \verb correctly, you'd have no problem: \verb|\citeyear{peel_epidemiology_2011}| Commented Oct 25, 2013 at 13:36
  • 2
    verbatim makes a text block the whole \linewidth wide; \verb<char><text><char> (where <char> is not used in <text> takes just the space it needs. See tex.stackexchange.com/questions/86568/… Commented Oct 25, 2013 at 13:40
  • 1
    @MarioS.E. Obviously: a verbatim environment doesn't make any sense in a cell declared to belong to an l column. Commented Oct 25, 2013 at 13:46

1 Answer 1

25

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

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.