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?

  • 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 '12 at 12:58
  • If you need multiple lines, \verb won't get you far. Does \begin{verbatim}...\end{verbatim} work? – krlmlr Feb 2 '12 at 13:19
  • 2
    It would be useful if you provided a Minimal Working Example. A package alltt might be useful. – yo' Feb 2 '12 at 17:20
  • 2
    You could use the subfigure environment, offered by the/my subcaption package. \verb+xxxx+ will work here. – user2574 Feb 4 '12 at 7:21
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.

|improve this answer|||||
-1

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}
|improve this answer|||||
  • 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. – Reinstate Monica - M. Schröder Feb 9 '13 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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