使用自訂 output-directory=build 的 LaTeX Minted 包

使用自訂 output-directory=build 的 LaTeX Minted 包

給定以下 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.pyg是在裡面的build目錄,但似乎找不到。

我能做些什麼?

答案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

我希望讀者不介意我擴展大衛卡萊爾的答案。多年來,我看到人們以多種方式提出這個問題。但是,我沒有看到提到專案的重要性小路載入 Minted 包時。

作為個人開發人員和作家,​​我最近從 TexStudio 改為嘗試 IntelliJ IDEA 的 TeXiFy 插件。我是在這樣的背景下寫的。我與這些產品沒有任何關係。

我透過設定解決 Minted 代碼列表異常\usepackage[outputdir=../auxil]{minted}

明特麥粒腫依賴Python Pygments套件,是編譯過程中的中間階段輔助輸出檔。流程:載入Latex包,開始編譯,用Minted包將程式碼片段編譯成中間編譯檔.pyg<...>,在PDF輸出時收集文件,完成PDF輸出處理。如果中間 .pyg<...> 不在預期資料夾中,則會發生 PDF 輸出異常。

使用 Latex minted.sty 套件編譯程式碼符合預設的基於根的資料夾結構。無論項目位於資料夾層次結構中的哪個位置,這都是預設值。預設 TeXiFy 插件不這樣做——它符合 IntelliJ IDEA Ultimate 的預設結構(或您選擇的 IDE),它不可避免地要遵守,就像插件一樣。因此,當 TeXiFy IDEA 插件編譯時,它會在錯誤的位置尋找 .pyg<...> 檔案。預設情況下,下面是簡單 MWE 的編譯結構...

鑄造輸出目錄層次結構

使用乳膠...

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

它產生異常...

錯誤:無法讀取infile:[Errno 2] 沒有這樣的檔案或目錄:『minted-mwe.pyg' C527 3284E8D3B9D57CD.pygtex

那是對的。不應在 \TexifyMintedTest\ 下搜尋 .pyg<...> 文件原始碼資料夾,它位於\輔助資料夾。

透過改變輸出目錄資料夾使用正確的檔案路徑載入 minted.sty 時,編譯過程將在正確的位置尋找 .pyg<...> 檔案...

鑄造輸出目錄 tex

HTH 非專家...

相關內容