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?
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?
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.
\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
_, &, etc. I.e. \codeline{a_b_c} will result in an error.
– strpeter
Jan 04 '14 at 16:20
underscore package by default in my docs and your example works perfectly with that.
– recluze
Jan 05 '14 at 13:04
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 |.
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}
#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
Use the following:
\newcommand{\codeline}[1]{\texttt{\detokenize{#1}}}
This will print #1 vermbatim.
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.
calendar-out.tex) containing only the output of cal and inside a verbatim environment.calendar-out.tex to png via the commands $ latex calendar-out.tex and $ dvipng -T tight calendar-out.
calendar-out.png into original LaTeX document via the graphicx package.
I hope this helps.
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.
verbatimcontent inside a macro. See Verbatim inside a command. You may be interested in what thelistingspackage has to offer instead. It also provides the means to create your own environments. For example, see Creating a custom environment. – Werner Dec 08 '12 at 06:18verbatiminside a macro the way I did it? – Paul S. Dec 08 '12 at 06:26fancyvrbpackage; it provides the\DefineVerbatimEnvironmentfor defining your own verbatim environment. There is also\DefineShortVerbfor creating short verbatim texts like this|int foo;|. – Stephan Lehmke Dec 08 '12 at 06:26\or%in code lines, maybe\newcommand{\codeline}[1]{\par{\ttfamily#1\par}}is enough? – Stephan Lehmke Dec 08 '12 at 06:36verbatimwork within …? – Werner Dec 08 '12 at 07:04\scantokensto re-parse the tokens, is complex, but is general) – user202729 Nov 16 '21 at 06:35