4

When I write the following

\subfloat[caption]{\label{fig:xxxx}\verb+xxxx+}

I get \verb illegal in command argument. Seems that isn't possible to define a verbatim environment inside a \subfloat.

When I use \subfloat[caption]{\label{fig:xxxx}\texttt{xxxx}} it works, but I need multiple lines, so I have to use verbatim

What alternatives do I have? Maybe another package?

4
  • 2
    If you don't really need verbatim mode and \texttt is sufficient, then \parbox{5cm}{\ttfamily text} will do. Adjust the dimension. Otherwise you should present an example.
    – egreg
    Feb 2, 2012 at 12:58
  • If you need multiple lines, \verb won't get you far. Does \begin{verbatim}...\end{verbatim} work?
    – krlmlr
    Feb 2, 2012 at 13:19
  • 2
    It would be useful if you provided a Minimal Working Example. A package alltt might be useful.
    – yo'
    Feb 2, 2012 at 17:20
  • 2
    You could use the subfigure environment, offered by the/my subcaption package. \verb+xxxx+ will work here.
    – user2574
    Feb 4, 2012 at 7:21

2 Answers 2

8

You can't use \verb in the argument to another command. If you really need verbatim mode in your subfloat, then something like

\newsavebox{\verbbox} % in the preamble

\begin{lrbox}{\verbbox}
\verb+xxxx+
\end{lrbox}%
\subfloat[caption]{\label{myfigure}\usebox{\verbbox}}

will do. Inside the lrbox you can also put a minipage, for longer verbatim snippets:

\begin{lrbox}{\verbbox}
\begin{minipage}{.3\textwidth}
\begin{verbatim}
Some verbatim
on more than one line
\end{verbatim}
\end{minipage}
\end{lrbox}%
\subfloat[caption]{\label{myfigure}\usebox{\verbbox}}

The subcaption package has features similar to subfig and subfloats are realized as environments, where verbatim is allowed.

0
0

See Section 6.9 ("How do I put a verbatim environment in a sub-float?") of the manual of the Subfig package. Basically, the manual suggests that you define the SubFloat environment in your preamble:

\makeatletter
\newbox\sf@box
\newenvironment{SubFloat}[2][]%
{\def\sf@one{#1}%
\def\sf@two{#2}%
\setbox\sf@box\hbox
\bgroup}%
{ \egroup
\ifx\@empty\sf@two\@empty\relax
\def\sf@two{\@empty}
\fi
\ifx\@empty\sf@one\@empty\relax
\subfloat[\sf@two]{\box\sf@box}%
\else
\subfloat[\sf@one][\sf@two]{\box\sf@box}%
\fi}
\makeatother

Then, use the SubFloat environment as follows:

\begin{figure}
\centering
\begin{SubFloat}[Black box]{\label{fig:label1}Caption 1.}%
\rule{4cm}{3cm}%...blackbox subfigure...
\end{SubFloat}%
\hspace{1.5cm}%
\begin{SubFloat}{\label{fig:label2}Caption 2.}%...verbatim subfigure with
\begin{minipage}[b]{0.3\linewidth}% a minipage to control the width...
\begin{verbatim}
abc def ghi jkl
xx x xxx
abc def ghi jkl
\end{verbatim}%
\end{minipage}%
\end{SubFloat}
\caption{Main Caption.}
\label{fig:label}
\end{figure}
1
  • 1
    No, while it's in the manual of a package, this is not the way to solve this problem - it's a very crude hack. Feb 9, 2013 at 22:48

You must log in to answer this question.

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