4

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?

3
  • 2
    A verbatim environment cannot go in the argument to another command.
    – egreg
    Commented Mar 14, 2019 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. Commented Mar 14, 2019 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=\\\{\}].
    – Piolie
    Commented Mar 15, 2019 at 1:46

1 Answer 1

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

You must log in to answer this question.

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