LaTeX Minted 包使用自定义 output-directory=build

LaTeX Minted 包使用自定义 output-directory=build

给定以下 LaTeX 文档 ( minimal.tex):

\documentclass{article}
\usepackage{minted}
\begin{document} 
\begin{minted}{c} 
int main() {
    printf("hello, world");
    return 0;
}
\end{minted} 
\end{document}

我可以用以下方法构建它:

pdflatex -shell-escape  minimal.tex

但我想将构建文件放在另一个目录中,因此我使用-output-directory=build,例如:

pdflatex -shell-escape -output-directory=build minimal.tex

但这样我得到:

...
(/usr/local/texlive/2016/texmf-dist/tex/latex/xcolor/xcolor.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/graphics-cfg/color.cfg)
(/usr/local/texlive/2016/texmf-dist/tex/latex/pdftex-def/pdftex.def
(/usr/local/texlive/2016/texmf-dist/tex/context/base/mkii/supp-pdf.mkii
[Loading MPS to PDF converter (version 2006.09.02).]
))) (./_minted-minimal/default-pyg-prefix.pygstyle)Error: cannot read infile: [Errno 2] No such file or directory: 'minimal.pyg'
system returned with code 256


! 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.

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

l.9 \end{minted}

实际上minimal.pygbuild目录里面,但是好像找不到。

我能做些什么?

答案1

您需要\usepackage[outputdir=build]{minted}告诉 minted 文件去了哪里。

答案2

您可以使用currfile-abspath(的子包currfile)自动设置 minted 的输出目录。必须在加载minted包之前完成。

\usepackage{currfile-abspath}
\getabspath{\jobname.log}
\ifthenelse{\equal{\theabsdir}{\thepwd}}% using ifthen package
%\ifdefstrequal{\theabsdir}{\thepwd}% using etoolbox package
    {}{\PassOptionsToPackage{outputdir=\theabsdir}{minted}}

显然无法可靠地与 MiKTeX 配合使用,请参阅https://github.com/gpoore/minted/issues/268

答案3

我希望读者不介意我扩展 David Carlisle 的回答。多年来,我见过很多人以各种方式提出这个问题。然而,我没有看到提到项目的重要性小路加载 Minted 包时。

作为一名个人开发者和作家,我最近从 TexStudio 转到了 IntelliJ IDEA 的 TeXiFy 插件。我就是在这个背景下写作的。我与这些产品没有任何关联。

我通过设置来解决 Minted 代码列表异常\usepackage[outputdir=../auxil]{minted}

Minted.sty依赖于Python Pygments包,以及编译过程中的中间辅助输出文件。流程:加载Latex包,开始编译,使用Minted包将代码片段编译成中间编译文件.pyg<...>,在PDF输出过程中收集该文件,完成PDF输出处理。如果中间.pyg<...>不在预期文件夹中,则会出现PDF输出异常。

使用 Latex minted.sty 包编译代码符合默认的基于根的文件夹结构。无论项目位于文件夹层次结构中的哪个位置,这都是默认结构。默认的 TeXiFy 插件不符合 - 它符合 IntelliJ IDEA Ultimate 的默认结构(或您选择的 IDE),它不可避免地必须遵守这些结构,就像插件一样。因此,当 TeXiFy IDEA 插件编译时,它会在错误的位置寻找 .pyg<...> 文件。默认情况下,下面是简单 MWE 的编译结构...

minted-outputdir-层次结构

使用 Latex......

\documentclass[11pt]{article}
% Packages
\usepackage{minted}
% Document
\begin{document}
    \begin{minted}{php}
    <?php
        $x = 1;
        if($x=1){
            echo "x=1";
        } else {
            echo "x=" . $x;
        }
    ?>
    \end{minted}
\end{document}
}

它会产生异常...

错误:无法读取文件:[Errno 2] 没有这样的文件或目录:“minted-mwe.pyg”找不到 P:\Writing-and-WritingSoftware\DevelopmentBooklets\VanElectrical\TexifyMintedTest\src_minted-minted-mwe\097AF5BD36CFF0D87D9991A6653D1FBAC2C2A1FBB8430C5273284E8D3B9D57CD.pygtex

没错。不应在 \TexifyMintedTest\ 下搜索 .pyg<...> 文件源码文件夹,它位于\辅助文件夹。

通过改变输出目录文件夹使用正确的文件路径当加载 minted.sty 时,编译过程将在正确的位置寻找 .pyg<...> 文件...

minted-outputdir tex

HTH 非专家...

相关内容