1

I am using Windows as OS and TeXstudio as editor.

My project has the following structure:

-project
    -main.tex
    -chapter1
        -chapter1.tex
        -bibgraf.bib
        -figures
        -listings
    -chapter2
        -chapter2.tex
        -bibgraf.bib
        -figures
        -listings

I am using the subfiles package in order to compile independently the various chapters of my document. I would like to use the minted package in each chapterN.tex file in order to list the codes present in the listings subdirectory of the chapterN directory.

I am not sure whether this is actually possible with minted, because it seems that it can only work with the root document directory according to what I have found here and here.

At the moment, compiling main.tex using chapter1/listing/pythonfile.py within \inputminted succeeds, however this generates an error when I compile chapter1.tex. The curious thing is that compiling chapter1.tex using ./listing/pythonfile.py has no effect on the output (the compilation seems to succeed bbut actually I do not notice any change in the .pdf output). Edit: I found out that for some reason the automatic update of the pdf output does not work anymore using minted, problem observed also here.

Is there a solution for this issue? If not, is there an alternative to minted that allows me to maintain the same project structure (and gives nice results with matlab and python codes possibly)? Edit: The issue remained is the fact that I have to use two different arguments for the inputminted command when compiling main.tex and chapter1.tex.

Below my MWE.

Main.tex

\providecommand{\main}{.}
\documentclass[11pt,reqno]{amsart}

\usepackage{subfiles}
\newcommand{\onlyinsubfile}[1]{#1}
\newcommand{\notinsubfile}[1]{}

\usepackage{float}
\usepackage{graphicx}

\def\biblio{\bibliographystyle{amsalpha}\bibliography{\main/bibgraf}}  % *Modification: added `\main/` to specify relative file location.

\graphicspath{{\main/figures/}}

\usepackage[justification=centering]{caption} % Centred captions

% Typesetting code
\usepackage[newfloat]{minted}
\captionsetup[listing]{position=top}
\newenvironment{longlisting}{\captionsetup{type=listing}}{}

\begin{document}
    \def\biblio{}
\renewcommand{\onlyinsubfile}[1]{}
\renewcommand{\notinsubfile}[1]{#1}

Hallo main

\subfile{chapter1/chapter1}

\bibliographystyle{amsalpha} 
\bibliography{chapter1/bibgraf}
\end{document}

chapter1.tex

%!TeX root = chapter1
\providecommand{\main}{..}

\makeatletter
\def\input@path{{\main/}}
\makeatother

\documentclass[\main/main.tex]{subfiles}

\graphicspath{
    {"\main/chapter1/figures/"}
    {"\main/\main/chapter1/figures/"}
}


\begin{document}
%% my chapter 1 content
Hallo, this is a test:
\begin{equation}
\label{eq:sec1:1}
\int dx =0 
\end{equation}

This is a figure test:
\begin{figure}[h]
    \includegraphics[width=0.5\textwidth]{example.pdf}
\end{figure}

\onlyinsubfile{this only appears if chapter1.tex is compiled (not when main.tex is compiled)}
\notinsubfile{this only appears if main.tex is compiled (not when chapter1.tex is compiled)}

This is a reference test: \cite{anderson}

This is a listing test:
\begin{longlisting}
    \caption{Source code of \texttt{pythonfile.py}, where the main classes are defined.}
    \inputminted[frame=single,xleftmargin=\parindent,linenos,breaklines,breakafter=_(\{\[\]]{python}{./listings/pythonfile.py}
\end{longlisting}\vspace{2cm}

\onlyinsubfile{Call reference}
\biblio
%% 
\end{document}
1
  • 1
    Note that subfiles 2019/10/25 v1.4 is able to handle locally included files (like images). There should be no need anymore to modify input and graphics pathes. This probably does not solve your issues, but the code should become simpler.
    – gernot
    Commented Oct 25, 2019 at 19:30

1 Answer 1

2

If you use a recent version of the subfiles package (v1.3 2019/09/28 or later) you can fix the problem by defining a command \subfix in the preamble, like that:

\makeatletter
\newcommand\subfix[1]{\import@path#1}
\makeatother

Then, if some error message indicates that a file is searched in the wrong directory, wrap the command around the name of the file. E.g., write

\inputminted{python}{\subfix{./listings/pythonfile.py}}

instead of just \inputminted{python}{./listings/pythonfile.py}.

Here is a complete example with the following file structure:

main.tex
sub/sub.tex
sub/listings/prime.py

where the files have the following contents:

% main.tex
\documentclass{article}
\usepackage{minted}
\makeatletter
\newcommand\subfix[1]{\import@path#1}
\makeatother
\usepackage{subfiles}
\begin{document}
\subfile{sub/sub}
\end{document}

% sub/sub.tex
\documentclass[../main]{subfiles}
\begin{document}
\inputminted{python}{\subfix{listings/prime.py}}
\end{document}

% sub/listings/prime.py
def prime(n):
    if n < 2:
        return False
    for i in range(2,n):
        if (n % i) == 0:
           return False
    return True

Running the command pdflatex -shell-escape main.tex in the main directory and pdflatex -shell-escape sub.tex in the subdirectory sub yields files main.pdf and sub/sub.pdf, respectively, both of which look like

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .