23

How do I create boldface and subscripts in verbatim mode? I've seen mention of a fancyvrb package that might be able to help. Can anyone provide working LaTeX code, either with or without fancyvrb?

1

2 Answers 2

25

Using the fancyvrb package, with the commandchars option you can introduce escape sequences in verbatim code; in particular, you can get boldfaced fonts (provided you are using a suitable font. Using the codes option you can specify catcode changes ; in particualr, this allows you to include formatted mathematics in verbatim text:

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{bera}

\begin{document}

\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`$=3\catcode`_=8}]
code line 1
$a_{i}$
code line 2
\textbf{boldfaced text}
\end{Verbatim}

\end{document}

enter image description here

Using the more powerful listings package, you can escape to LaTeX using the escapeinside option and you can activate the mathescape option:

\documentclass{article}
\usepackage{listings}
\usepackage{bera}

\lstset{basicstyle=\ttfamily,
escapeinside={||},
mathescape=true}

\begin{document}

\begin{lstlisting}
code line 1
$a_{i}$
code line 2
|\textbf{boldfaced text}|
\end{lstlisting}

\end{document}

enter image description here

The above solutions treted subscripts as mathematical expressions; you can get non-math subscripts using \textsubscript from the fixltx2e package; a little example using fancyvrb:

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{fixltx2e}
\usepackage{bera}

\begin{document}

\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`$=3\catcode`_=8}]
code line 1
$a_{i}$
code line 2
\textbf{boldfaced text}
a\textsubscript{i}
\end{Verbatim}

\end{document}

enter image description here

5
  • 1
    Thanks for the working code. I noticed the subscripts require math mode. Is there any way to keep subscripts in the same fixed-width font (e.g. Courier) as the verbatim, non-math text? Commented Jul 19, 2012 at 5:01
  • @stackoverflowuser2010 sure; please see the last code in my updated answer for an example. Commented Jul 19, 2012 at 5:18
  • Thanks for your help. Now I need to figure out what those 'catcode' sequences mean. Are those from the fancyvrb package? Commented Jul 19, 2012 at 6:54
  • @stackoverflowuser2010 Catcodes come from TeX. All characters in TeX are assigned a "category code" or catcode. Normal characters are catcode 11; this category normally comprises all of the letter characters. Some special characters have other catcode which means they are not treated as a normal letter. Internally fancyvrb "annihilates" special catcodes to treat all characters as normal letters. When you use the option \catcode_=8, for example, you are instructing fancyvrb` to treat the underscore not as a regular letter, but as the special character used to introduce subscripts. Commented Jul 19, 2012 at 12:59
  • @stackoverflowuser2010 A detailed explanation on catcodes can be found in TeX by Topic. Please, don't forget to accept my answer if you consider that it solved your problem. Commented Jul 19, 2012 at 13:00
0

You can use the alltt package, which offers an alltt environment that is like verbatim, except that \{} are still metacharacters. Remember that subscripts and superscripts are also available via \sb and \sp and $...$ can also be written as \(...\), so you don't need $, _ and ^ as (unavailable) metacharacters.

Example (from slide 19 here):

\begin{alltt}
  for \(i:=1,\ldots,n\)
    print \(x\sb{i}\sp{2}\)
\end{alltt}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .