0

I have a document that is constructed from a number of *.tex files stored in a directory organized as follows:

-2022
   -07
      -01
         -note.tex
         -file1.pdf
         -differentfilename.pdf
      -02
         -note.tex
      -05
         -note.tex
         -somefile.pdf
         -anotherfile.pdf
etc.

My "master" LaTeX document loops through all of the date folders and includes the note.tex into the document. I'd like to include the various pdf files as links to open in the system reader if they are present on a given day. Is there a way I can get LaTeX to loop through the file structure and include pdf's as appropriate? My MWE attempt is as follows:

% !TEX options=--shell-escape
\documentclass{article}
\usepackage[letterpaper,margin=1.25in,top=1in,bottom=1in]{geometry}
\usepackage{pgfcalendar}
\let\d=\pgfcalendarshorthand
\newcommand\formatdate[2]{\pgfcalendar{cal}{#1}{#1}{#2}}

\newcommand\firstdate{2022-07-01}
\newcommand\lastdate{\year-\month-\day}

\begin{document}

\pgfcalendar{cal}{\firstdate}{\lastdate}{%
    \def\formatteddate{\d{y}0/\d{m}0/\d{d}0}%
    \graphicspath{ {./\formatteddate/}}     
    \IfFileExists{./\formatteddate/note.tex}{
        \marginpar{\vspace*{1em}\textsf{ \d m. \d d- \d y- }}%
        \vspace*{1.5em}
        \addcontentsline{toc}{section}{\d mt \d d0, \d y0}%
        \input{./\formatteddate/note.tex}
        \begin{flushright}
            HERE IS WHERE I'D LIKE SOME COMMAND THAT CAN FIND ANY PDFS IN THE DAY'S FOLDER AND INCLUDE A LINK TO THEM IF POSSIBLE.
        \end{flushright}

    }{}
}


\end{document}

I guess it's fairly easy to do this if the file names followed some convention, but in my case they do not, so I'd like to identify them by the .pdf extension and include links to any such files. Thank you!

5
  • 2
    This really feels like something that is easier done using a shell script. Commented Jul 8, 2022 at 1:54
  • Is there a way to get the shell script to run every time the TeX documents gets built?
    – pQ12branch
    Commented Jul 8, 2022 at 2:28
  • Why do that? Shell scripts are designed to execute commands. So use your shell script to (1) first generate your tex file and when it is done (2) run latexmk or whatever to build the pdf output. Commented Jul 8, 2022 at 13:23
  • I second the comment of @WillieWong. Personally, I feel that it is better to invest your time in developing a skill in a computer programming language, like Python, Java, or C, and have all processing controlled through the computer program. Then, instead of executing pdflatex, you would execute the computer program, perhaps feeding it the *.tex filename, as a parameter. Extending the idea, if you are using a programmer's editor, like Emacs, Vi, or one of the Vi derivatives, it should be a simple matter to add a menu option to the editor's gui that runs a program. ...see next comment Commented Aug 7, 2022 at 19:11
  • In fact, if you are using (for example) Emacs, then the Emacs menu option could invoke an Emacs-Elisp file. One advantage of this is that inside the Emacs editing buffer, Emacs knows the *.tex file name. I suspect that it also knows the absolute path of the file in the editing buffer. Commented Aug 7, 2022 at 19:14

1 Answer 1

2

To make my shell script suggestion precise: work in the parent directory to 2022.

  1. Create a .tex file (say preamble.tex) that contains all the preamble material (up to \begin{document}).
  2. Create the following buildscript.sh (untested; maybe typos)
#!/bin/bash
cat preamble.tex > main.tex
for filedate in */*/*
do
###  Insert lines here such us your margin par and ToC commands
    echo "\input{./${filedate}/notes.tex}" >> main.tex
    if compgen -G $filedate/*.pdf >/dev/null; then
        for pdffile in $filedate/*pdf
        do
            ### Change this line to format it or whatever
            echo "\href{run:./${pdffile}}" >> main.tex
        done
    fi
done
echo "\end{document}" >> main.tex
latexmk main.tex

If you run buildscript.sh it should, for each date, first input the corresponding notes.tex and then create a hyperlink (make sure to load hyperref in your preamble) to every pdf file in that directory. And after creating the TeX file it will build it for you.


Quick explanation:

  • The script above assumes a directory structure year/month/day as you indicated.
  • It assumes every day will have a notes.tex; you can add an existence check if you want.
  • It does not assume there are any PDF files. The compgen -G command is a bash specific builtin (hence #!/bin/bash) that returns true if the glob pattern has non-zero matches. So no hyperlinks are created if there are no PDF files in the directory.

You must log in to answer this question.

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