Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
1  
Look at this tex.stackexchange.com/questions/33236/… –  R. Schumacher Jul 19 '12 at 1:12

1 Answer 1

up vote 11 down vote accepted

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

share|improve this answer
    
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? –  stackoverflowuser2010 Jul 19 '12 at 5:01
    
@stackoverflowuser2010 sure; please see the last code in my updated answer for an example. –  Gonzalo Medina Jul 19 '12 at 5:18
    
Thanks for your help. Now I need to figure out what those 'catcode' sequences mean. Are those from the fancyvrb package? –  stackoverflowuser2010 Jul 19 '12 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. –  Gonzalo Medina Jul 19 '12 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. –  Gonzalo Medina Jul 19 '12 at 13:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.