在子目录中的子文件中使用 minted

在子目录中的子文件中使用 minted

答案在这里给出了一个 MWE,展示了如何\inputminted在子文件中工作。但是,该方法不适用于\begin{minted}。对于以下设置

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}

孤立构建sub/sub.tex(通过 overleaf)会导致错误:

(./_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.

main.tex在 MWE 中,建筑似乎运行良好。

我怎样才能使建筑物sub/sub.tex正常运转?

答案1

尝试这个:

% 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}

相关内容