Realize Inline Code Block in LaTeX: \verb
, \verb*
, \texttt
, vs. \lstinline
of listings
Package
In LaTeX, we can use command \verb
or \verb*
to insert an inline code block among the main text1, and the syntax is2:
1
2
\verb char literal-text char
\verb* char literal-text char
where char
can be |
, !
, +
, _
, or "
et al., for example,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
\documentclass{article}
\usepackage[x11names]{xcolor}
\pagecolor{AntiqueWhite1}
\setlength\parindent{0pt}
\begin{document}
This is a \verb|latex command \LaTeX|.\\
This is a \verb!latex command \LaTeX!.\\
This is a \verb+latex command \LaTeX+.\\
This is a \verb_latex command \LaTeX_.\\
This is a \verb"latex command \LaTeX".\\
This is a \verb#latex command \LaTeX#.\\
This is a \verb@latex command \LaTeX@.\\
This is a \verb*|latex command \LaTeX|.\\
This is a \verb*!latex command \LaTeX!.\\
This is a \verb*+latex command \LaTeX+.\\
This is a \verb*_latex command \LaTeX_.\\
This is a \verb*"latex command \LaTeX".\\
This is a \verb*#latex command \LaTeX#.\\
This is a \verb*@latex command \LaTeX@.\\
\end{document}
As can be seen,
- the
literal-text
wrapped in thechar
s is displayed in typewriter font; - compared to
\verb
, the spaces in\verb*
are printed with a visible space character.
However, these two commands are fragile2, which would cause errors in many circumstances. Here are three examples.
(1) In \section
command
1
2
3
4
5
6
7
8
9
10
\documentclass{article}
\usepackage[x11names]{xcolor}
\pagecolor{AntiqueWhite1}
\setlength\parindent{0pt}
\begin{document}
\section{\verb*|latex command \LaTeX|}
\end{document}
1
2
3
4
5
6
7
8
9
10
11
line 8: \verb illegal in argument. \section{\verb*|latex command \LaTeX|}
line 8: Argument of \@sect has an extra }. \section{\verb*|latex command \LaTeX|}
line 8: Paragraph ended before \@sect was complete. \section{\verb*|latex command \LaTeX|}
line 10: Argument of \@gobble has an extra }. \end{document}
line 10: Paragraph ended before \@gobble was complete. \end{document}
line 10: Argument of \@gobble has an extra }. \end{document}
line 10: Paragraph ended before \@gobble was complete. \end{document}
line 10: Argument of \@firstoftwo has an extra }. \end{document}
line 10: Paragraph ended before \@firstoftwo was complete. \end{document}
line 10: Missing \endcsname inserted. \end{document}
...
(2) In \textcolor
command
1
2
3
4
5
6
7
8
9
10
\documentclass{article}
\usepackage[x11names]{xcolor}
\pagecolor{AntiqueWhite1}
\setlength\parindent{0pt}
\begin{document}
\textcolor{red}{\verb*|latex command \LaTeX|}
\end{document}
1
line 8: \verb illegal in argument. \textcolor{red}{\verb*|latex command \LaTeX|}
(3) In \makebox
command
1
2
3
4
5
6
7
8
9
10
\documentclass{article}
\usepackage[x11names]{xcolor}
\pagecolor{AntiqueWhite1}
\setlength\parindent{0pt}
\begin{document}
\makebox[10pt]{\verb|latex command \LaTeX|}
\end{document}
1
2
3
line 8: Missing } inserted. \makebox[10pt]{\verb|latex command \LaTeX|}
line 8: Extra }, or forgotten \endgroup. \makebox[10pt]{\verb|latex command \LaTeX|}
line 8: \verb illegal in argument. \makebox[10pt]{\verb|latex command \LaTeX|}
The reason is that we can’t use \verb
and \verb*
as a part of setting an argument of a macro2:
You cannot use \verb
in the argument to a macro, for instance in the argument to a \section
. It is not a question of \verb
being fragile (see \protect
3), instead it just cannot work, as the \verb
command changes the catcode regime before reading its argument, and restore it immediately afterward, nevertheless with a macro argument the content of the argument has already be converted to a token list along the catcode regime in effect when the macro was called. However, the cprotect
package4 can help with this.
For the \section
command, we could use \cprotect
command provided by cprotect
package4 to solve the problem:
1
2
3
4
5
6
7
8
9
10
11
12
\documentclass{article}
\usepackage[x11names]{xcolor}
\pagecolor{AntiqueWhite1}
\usepackage{cprotect}
\setlength\parindent{0pt}
\begin{document}
\cprotect\section{\verb|latex command \LaTeX|}
\end{document}
but it’s not workable for \textcolor
and \makebox
et al. commands.
Commands \verb
and \verb*
are not professional for inline code block designed on purpose, but many writers get used to using them to display code to contrast with the surrounded main text. In fact, they are respectively inline version of verbatim
and verbatim*
environment, which is literally for verbatim transcription. This accounts for, to some extent, why they are not compatible with other macros.
The verbatim
environment is a paragraph-making environment that gets LaTeX to print exactly what you type in. It turns LaTeX into a typewriter with carriage returns and blanks having the same effect that they would on a typewriter. The output looks exactly as it looks in the input file.5
like6:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
\documentclass{article}
\usepackage[x11names]{xcolor}
\pagecolor{AntiqueWhite1}
\setlength\parindent{0pt}
\begin{document}
\begin{verbatim}
#!usr/bin/env perl
$name = "guy";
print "Hello, $name!\n";
\end{verbatim}
\begin{verbatim*}
#include <stdio.h>
main() {
printf("Hello, world.\n");
}
\end{verbatim*}
\end{document}
To avoid the problem caused by \verb
in some cases, we can utilize \texttt
command to realize the same visual effect, i.e. displaying the text in the typewriter font7:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
\documentclass{article}
\usepackage[x11names]{xcolor}
\pagecolor{AntiqueWhite1}
\setlength\parindent{0pt}
\begin{document}
\section{\texttt{latex command \textbackslash LaTeX}}
\textcolor{red}{\texttt{latex command \textbackslash LaTeX}}\\
\makebox[10pt]{\texttt{latex command \textbackslash LaTeX}}
\end{document}
but we should note the way of dealing with some special characters in each programming language, like use \textbackslash
to output \
in the above example.
Besides, the \lstinline
command of listings
package is also could be used to show inline code8:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
\documentclass{article}
\usepackage[x11names]{xcolor}
\pagecolor{AntiqueWhite1}
\usepackage{cprotect}
\setlength\parindent{0pt}
\usepackage{listings}
\lstset{basicstyle=\ttfamily} % Note here.
\begin{document}
\lstinline{latex command \LaTeX}
\cprotect\section{\lstinline{latex command \LaTeX}}
\textcolor{red}{\lstinline{latex command \LaTeX}}
\makebox[10pt]{\lstinline{latex command \LaTeX}}
\end{document}
1
2
line 16: Improper alphabetic constant. ...lor{red}{\lstinline{latex command \LaTeX}}
line 18: Improper alphabetic constant. ...ox[10pt]{\lstinline{latex command \LaTeX}}
but as shown, this command will cause similar errors as \verb
(and its syntax also resembles \verb
9).
References