4

I previously wrote a computer professional book in which I showed what the user typed on the console and the computer's resultant output. The way I liked to do this was with everything in courier and with the user's input in bold. Something like this:

<tt>
$ <b>echo foo</b>
foo
$
</tt>

I typed this as HTML because I don't know how to do this in Markdown (what StackExchange uses) and I don't know how to easily do it in LaTeX. I can certainly do it with \texttt{\$ \textbf{echo foo}}, but I would rather not have to quote everything.

I don't want to use lstlisting as recommended by Using bold/italic text inside listings?, because I don't want LaTeX deciding what's bold.

Any other recommendations?

8

If you only need fancyvrb and not the full fledged listings package, you can do it quite easily with the commandchars option:

\documentclass{article}
\usepackage{fancyvrb}
\newcommand\userinput[1]{\textbf{#1}}
\usepackage{tgcursor}

\begin{document}
\begin{Verbatim}[commandchars=\\\{\}]
$ \userinput{echo foo}
foo
$
\end{Verbatim}
\begin{Verbatim}[commandchars=\#\[\]]
$ #userinput[echo \]
> #userinput[foo]
foo
$
\end{Verbatim}
\end{document}

The triple of escaped characters can consist of anything is not used in the Verbatim environment.

enter image description here

2

Another option, that is less flexible than egreg's, is to use the alltt package.

\documentclass{article}
\usepackage{alltt}
\begin{document}
\begin{alltt}
$ {\bf echo foo}
foo
$
\end{alltt}
\end{document}
  • 1
    This makes the section bold, but it's not in typewriter font. – Charles Clayton Oct 23 '16 at 3:29
  • @crclayton True. To correct that, it would in principle be enough to change {\bf to \textbf{, but in practice this gives non-bold typewriter font, likely because the appropriate combination is not found. egreg fixes that by adding the package tgcursor. – Bruno Le Floch Oct 23 '16 at 10:50

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