0

I have nested folders with multiple files containing statements and solutions for exercices.

main.tex
exercices (folder)
⮑ statements (folder)
   ⮑  1.tex
   ⮑  2.tex
⮑ solutions (folder)
   ⮑  2.tex

From what I understand on few topics like input files in different directories, you cannot input files from different nested directories and need to use the import package. So this was the previous code :

\foreach \filenumber in {1,2}{
  \IfFileExists{exercices/statements/\filenumber}{\input{exercices/statements/\filenumber}}
  \IfFileExists{exercices/solutions/\filenumber}{\input{exercices/solutions/\filenumber}}
}

The issue is that the following code still doesn't work with the \import command.

\documentclass{book}
\usepackage{import}
\usepackage{tikz}

\begin{document}
\foreach \filenumber in {1,2}{
  \IfFileExists{exercices/statements/\filenumber}{\import{exercices/statements/}{\filenumber}}
  \IfFileExists{exercices/solutions/\filenumber}{\import{exercices/solutions/}{\filenumber}}
  }
\end{document}

The code gives the error LaTeX Error: File exercices/solutions/1.tex' not found.

4
  • "From what I understand on few topics like input files in different directories, you cannot input files from different nested directories and need to use the import package." ?? you can always use \input, you may find \import helps with the syntax in some cases, although personally I think it makes things more complicated for little gain. Commented Jun 28, 2023 at 10:43
  • \IfFileExists{exercices/statements/\filenumber}{\input{exercices/statements/\filenumber}} can be simplified and made more efficient by \InputIfFileExists{exercices/statements/\filenumber} Commented Jun 28, 2023 at 10:44
  • @DavidCarlisle \foreach \filenumber in {1,2}{\InputIfFileExists{exercices/statements/\filenumber} \InputIfFileExists{exercices/solutions/\filenumber}} inputs 1.tex and 2.tex from the statements folder but it doesn't input 2.tex from the solutions folder. I did not do anything in particular about the start, I'm using overleaf so I guess it starts from main.tex as usual.
    – Flowt
    Commented Jun 28, 2023 at 10:59
  • 2
    Oh you are missing the yes and no arguments from the call see my {}{No solution} Commented Jun 28, 2023 at 11:03

1 Answer 1

2

You could use \input but here you simply want \InputIfFileExists

\documentclass{book}

\usepackage{pgffor}

\begin{document}
\foreach \filenumber in {1,2}{%
  \InputIfFileExists{exercices/statements/\filenumber}{}{}\par
  \InputIfFileExists{exercices/solutions/\filenumber}{}{No solution}\par
  }
\end{document}

The terminal and log show the three files being input:

(./exercices/statements/1.tex) (./exercices/solutions/1.tex)
(./exercices/statements/2.tex) [1

Producing

enter image description here

If you put suitable text such as "ex1" in 1.tex

You must log in to answer this question.

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