# Directory for importing minted snippets

Is there a way to tell minted to import the snippets from a certain directory? I'm looking for something similar to the way \graphicspath{{img/}} works for images.

Right now, all the files I need to import get mixed with my other .tex files.

The feature is not available in the current minted. Redefining \inputminted is out of the question, I'm afraid, because it has different definitions according to whether draft is on or off.

Anyhow, you can quite easily define a wrapper around \inputminted. The syntax of \pathinputminted is the same as \inputminted:

\pathinputminted[<options>]{<language>}{<filename>}

Full code

\documentclass{article}
\usepackage{minted}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\mintedpath}{m}
{
\seq_gset_split:Nnn \g_paulie_mintedpath_seq { } { #1 }
\seq_gput_left:Nn \g_paulie_mintedpath_seq { }
}

\seq_new:N \g_paulie_mintedpath_seq

\NewDocumentCommand{\pathinputminted}{O{}mm}
{
\seq_map_inline:Nn \g_paulie_mintedpath_seq
{
\file_if_exist:nT { ##1 #3 }
{
\inputminted[#1]{#2}{##1 #3}
\seq_map_break:
}
}
}
\ExplSyntaxOff

\mintedpath{ {sub1/} {sub2/} }

\begin{document}

\pathinputminted{latex}{test1.tex} % this is only in sub1

\pathinputminted{latex}{test2.tex} % this is both in sub1 and sub2

\pathinputminted{latex}{test3.tex} % this is only in sub2

\pathinputminted{latex}{test4.tex} % this is both in current directory and sub1

\end{document}


This is the directory structure I set up for the purpose of the example, after the document has been typeset once, so the Pygments files are already shown.

paulie > ls -R
_minted-paulie/ paulie.log  paulie.tex  sub2/
paulie.aux  paulie.pdf  sub1/       test4.tex

./_minted-paulie:
24A365234AE4FDB0DF45E303775CC522F86A94839C3F0079C692F025CF998383.pygtex
4CF555EB3B29AA33E0D843E51AE34A9128809557B179BB64CA75D7BABAABEFE2.pygtex
D3967FFDA37461EF76D39CC2F8B40FCC93A75DBBC5197FD7555A181B29CF5ABC.pygtex
FD1CB1B77BD8698379A00F3A3BED29F678CA5D356D6BB9C6FD6DCBC2BA6DFEA3.pygtex
default-pyg-prefix.pygstyle
default.pygstyle

./sub1:
test1.tex   test2.tex

./sub2:
test2.tex   test3.tex   test4.tex


As you see from the output

only the first hit is input. I added an implicit ./ at the beginning of the search paths, so a file in the working directory is found first and, like in the other cases, further searching is not performed.

• Is there a way to use your solution with the \newmintedfile command? Mar 10 '19 at 21:39
• @Paulie I'd say no. Mar 10 '19 at 22:00

You can do it with \inputminted[<options>]{<language>}{<filename>}.

In <filename> you can indicate the complete path (remember to use \ (not /) also for Windows paths.

If you want to set a fixed path, you can simple create a new command, for example:

\documentclass{article}

\usepackage{minted}
\newcommand{\myminted}[1]{\inputminted{latex}{/my/dir/#1}}

\begin{document}

\myminted{main.tex}

\end{document}

• Thank you, but I already know how to do that. I'm asking if there is a way to declare the folder (\mintedpath{my/dir/}, for example), so that I can just write \inputminted{<language>}{snippet.snip} to import the snippet. Mar 9 '19 at 21:25
• @Paulie Just create a new command, see my renewed answer. Mar 9 '19 at 21:31
• I like your approach, but a new command would have to be defined for every minted language and set of options, right? Mar 10 '19 at 21:20
• @Paulie Alternatively, you can define a new command with more than one parameter: one for the file name, the others for the other opinions. Mar 11 '19 at 8:29