12

I'm trying to write a \newcommand for verbatim:

\newcommand{\codeline}[1]{\begin{verbatim}{#1}\end{verbatim}}

and use it as

\codeline{int foo;}

but it gives an error "File ended while scanning use of \@xverbatim". How to fix it?

Paul S.
  • 1,169

5 Answers5

8

If you want a simple implementation, go with

\newcommand[1]{{\small\texttt{#1}}}

If you want more detailed control, you should go with fancyvrb or minted packages.

recluze
  • 4,201
  • Oh, btw, I like to put a \small in there because the default monospace font is a little too big for my taste. I like my code snippets relatively equal to or smaller than my main font. – recluze Dec 08 '12 at 11:45
  • 3
    This "dirty" command does not work for special caracters as _, &, etc. I.e. \codeline{a_b_c} will result in an error. – strpeter Jan 04 '14 at 16:20
  • 2
    @strpeter I use the underscore package by default in my docs and your example works perfectly with that. – recluze Jan 05 '14 at 13:04
3

Specific answer

For my example below the following code from the listings package was the solution:

\newcommand{\codeline}[1]{\lstinline|#1|}

Of course this is only reasonable if you are using short inline code examples without vertical line |.

Example

MWE in which this implementation makes sense:

\documentclass[landscape]{article}
\usepackage{listings}
\newcommand{\includegraphicswithtitle}[1]{%
\clearpage%
\section*{\lstinline|#1|}%
\includegraphics{#1}}

\title{All graphics of my thesis}

\begin{document}
\maketitle
\centering
\includegraphicswithtitle{Image08_a_1.pdf}
\includegraphicswithtitle{Image08_b_1.pdf}
\includegraphicswithtitle{Image08_c_1.pdf}
\end{document}
strpeter
  • 5,215
  • 3
    What's the advantage of \codeline{text} over \lstinline|text|? – egreg Jan 04 '14 at 16:47
  • 1
    @egreg: You can crop several lines together in order to reduce repeating code. Is this not advantage enough? If there is a more efficient answer, please feel free to correct me. – strpeter Jan 04 '14 at 17:06
  • Also note that in this case #1 will be tokenized first (then detokenized), so \abc\def may become \abc \def, and you can't include % or ignored characters or multiple consecutive spaces. – user202729 Nov 16 '21 at 06:28
  • Correction, it's possible to include special characters in \lstinline (but it needs to be appropriately escaped), read its documentation for more details. – user202729 Mar 30 '22 at 14:41
2

Use the following:

\newcommand{\codeline}[1]{\texttt{\detokenize{#1}}}

This will print #1 vermbatim.

Alkis
  • 101
0

I met the same problem when I tried to include the output of the Unix cal command inside a LaTeX macro. Enclosing the cal output within the verbatim environment does not work for reasons cited above.

My solution is a hack.

  1. Create a LaTeX document (calendar-out.tex) containing only the output of cal and inside a verbatim environment.
  2. Convert calendar-out.tex to png via the commands $ latex calendar-out.tex and $ dvipng -T tight calendar-out.
  3. Incorporate calendar-out.png into original LaTeX document via the graphicx package.

I hope this helps.

ChrisS
  • 13,529
Robert
  • 1
0

Using Ulrich Diez's \DefineVerbatimToScantokens macro from the answer https://tex.stackexchange.com/a/629173/250119 (note. go to that answer to read on how to use that macro & maybe upvote if you find the answer useful. Unfortunately as far as I know it's not in any package at the moment so you have to copy paste the code), we can do the following:

\documentclass{article}

%=== Code of \DefineVerbatimToScantokens ======================== % ... <copy the code from that answer> %=== End of code of \DefineVerbatimToScantokens =================

\DefineVerbatimToScantokens\codeline{v}{% \begin{verbatim} #1 \end{verbatim} }%

\begin{document}

\codeline{int foo;} \codeline{xy%#?!z}

\end{document}

Also read documentation of xparse.pdf to see what does it mean to specify v here and that it allows % etc. except literal TAB to be included in the argument.

user202729
  • 7,143
  • Also note that performance of this solution might be worse than \texttt solution because of the need to rescan the tokens each time it's called. – user202729 Mar 30 '22 at 14:48