4

The answer here gives a MWE that shows how to make \inputminted work in a subfile. However, the approach does not work for \begin{minted}. For the following setup

main.tex:

\documentclass{article}
\usepackage{minted}
\usepackage{subfiles}
\begin{document}
\subfile{sub/sub}
\end{document}

sub/sub.tex

\documentclass[../main]{subfiles}
\begin{document}
\begin{minted}{python}
print("Hello2 world")
\end{minted}
\end{document}

building sub/sub.tex in isolation (via overleaf) results in the error:

(./_minted-output/default-pyg-prefix.pygstyle) (./_minted-output/default.pygsty
le)
runsystem(pygmentize -l python -f latex -P commandprefix=PYG -F tokenmerge -o _
minted-output/ED80FDFC2E78B411DE97C895BDF46CCE50EF3049E275D339AF879205FC52C1A0.
pygtex output.pyg)...executed.

! Package minted Error: Missing Pygments output; \inputminted was probably given a file that does not exist--otherwise, you may need the outputdir package option, or may be using an incompatible build tool, or may be using frozencache with a missing file.

See the minted package documentation for explanation. Type H <return> for immediate help. ...

l.5 \end{minted}

This could be caused by using -output-directory or -aux-directory without setting minted's outputdir, or by using a build tool that changes paths in ways minted cannot detect, or using frozencache with a missing file.

Building main.tex seems to work fine in the MWE.

How can I make building sub/sub.tex work too?

Eric
  • 669

1 Answers1

2

Try this:

% main.tex
\documentclass{article}
\usepackage{minted}
\usepackage{subfiles}

\usepackage{xpatch}

\makeatletter % fix for first kind \xpatchcmd\inputminted {\minted@pygmentize[#3]{#2}} {\minted@pygmentize[\import@path #3]{#2}} {}{\fail}

\makeatother

\begin{document} \subfile{sub/sub} \end{document}

% sub/sub.tex
\documentclass[../main]{subfiles}
\begin{document}
\begin{minted}{python}
print("Hello2 world")
\end{minted}

\inputminted{python}{./pythonfile.py} % a dummy python file located in sub/pythonfile.py
\end{document}
muzimuzhi Z
  • 26,474
  • Seems to do the trick, but only if xpatchcmd is included after subfiles. – Eric Aug 14 '20 at 09:10
  • Is this a bug in one of the two packages in your opinion? – Eric Aug 14 '20 at 09:10
  • @Eric Kind of compatibility issue, but not bug. See similar issue between minted and import packages: https://github.com/gpoore/minted/issues/233. – muzimuzhi Z Aug 14 '20 at 09:25
  • This is no longer working for me; it seems to only work if you build the main.tex first; that is, sub/sub.tex is able to read from the cache, but it is not able to write to it. – Eric Sep 22 '23 at 16:00