3

I've seen some similar questions here and here, but none mention the \subcaptionbox command. Here is my MWE:

\documentclass{article}
\usepackage{subcaption}
\usepackage{minted}

\begin{document}
    \begin{figure}[htb]
        \subcaptionbox{One subfigure.}{%
            \begin{minted}{c}
                printf("hello, world\n");
            \end{minted}
        }
    \end{figure}
\end{document}

This fails to compile with a Paragraph ended before \FV@BeginScanning was complete. } error.

I could use a minipage environment and the \subcaption command instead, but \subcaptionbox has some advantages that I'd like to use.

Is there any way to make these two work together?

  • 2
    A verbatim environment cannot go in the argument to another command. – egreg Mar 14 at 7:38
  • use the minted form that inputs a file and put your code fragments in external files so they are not contained in the argument of another command. – David Carlisle Mar 14 at 7:52
  • @DavidCarlisle I tried using \subcaptionbox{One subfigure.}{\inputminted{c}{helloWorld.c}} but still get errors: Something's wrong--perhaps a missing \item. \begin{Verbatim}[commandchars=\\\{\}]. – Paulie Mar 15 at 1:46
2

Here's a crude implementation, with just the standard form for \subcaptionbox (no optional arguments).

\documentclass{article}
\usepackage{subcaption}
\usepackage{minted}

\newsavebox{\mintedbox}
\newenvironment{mintedsubcaptionbox}[2]
 {%
  \VerbatimEnvironment
  \def\mscbcaption{#1}%
  \RecustomVerbatimEnvironment{Verbatim}{BVerbatim}{}%
  \begin{lrbox}{\mintedbox}%
  \begin{minted}{#2}%
 }
 {%
  \end{minted}%
  \end{lrbox}%
  \subcaptionbox{\mscbcaption}{\usebox{\mintedbox}}%
 }

\begin{document}

\begin{figure}[htb]

\begin{mintedsubcaptionbox}{One subfigure.}{c}
printf("hello, world\n");
\end{mintedsubcaptionbox}

\end{figure}

\end{document}

I used BVerbatim because it's quite unlikely that you want a listing to be full width inside a subfloat.

enter image description here

A fuller support with syntax

\begin{mintedsubcaptionbox}[<list entry>]{<heading>}[<minted options>]{<language>}

can be

\documentclass{article}
\usepackage{subcaption}
\usepackage{minted}
\usepackage{xparse}

\newsavebox{\mintedbox}

\NewDocumentEnvironment{mintedsubcaptionbox}{O{#2}mO{}m}
 {%
  \VerbatimEnvironment
  \RecustomVerbatimEnvironment{Verbatim}{BVerbatim}{}%
  \begin{lrbox}{\mintedbox}%
  \begin{minted}[#3]{#4}%
 }
 {%
  \end{minted}%
  \end{lrbox}%
  \subcaptionbox[#1]{#2}{\usebox{\mintedbox}}%
 }

\begin{document}

\begin{figure}[htb]

\begin{mintedsubcaptionbox}{One subfigure.}{c}
printf("hello, world\n");
\end{mintedsubcaptionbox}

\end{figure}

\end{document}
  • Thank you. Is this solution too hacky to be included in the minted package out-of-the-box? Maybe I could suggest it. – Paulie Mar 15 at 1: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.