0

My thesis supervisor requires me to include all my MATLAB code in the PDF. My code is scattered in a dozen .m files, all in the same folder.

I know I can include code, using the matlab-prettifier package, and I do so as follows:

\lstinputlisting[style=Matlab-editor]{codice/ICCplot.m}

In essence, I want to do the following:

for \filename in codice/*.m
    \section{\filename}
    \lstinputlisting[style=Matlab-editor]{codice/\filename.m}
end for
  • 2
    A dozen is 12, right? Unless the filenames are very complicated I would suggest to do this manually, just copy-paste it 12 times and change the filenames. Otherwise you can use a scripting language of your choice (for example MATLAB itself) to produce the 2*12 lines of LaTeX code with the correct filenames automatically, and copy the resulting code to your document. – Marijn Jun 25 '21 at 08:25
  • I would have liked the freedom to rename, add, remove files without worrying about having to run a separate tool. Oh well. I guess I could write a Bahs script and find a way to have it run before every compilation. – Riccardo Orlando Jun 25 '21 at 08:28
  • 1
    Write yourself a Makefile, call LaTeX with that Makefile, add some wildcard matching to your Makefile to pass the list of files to LaTeX. – Skillmon Jun 25 '21 at 10:14
  • Yes, thanks. Do I have to mark my question as duplicate? – Riccardo Orlando Jun 25 '21 at 10:43

1 Answers1

0

I'd do this with a Makefile, a very simple one is the:

TEX=pdflatex
NAME=riccardos_thesis
MFILES=$(wildcard codice/*.m)

all: $(NAME).pdf

$(NAME).pdf: $(NAME).tex $(MFILES) $(TEX) -jobname="$(NAME)" "\def\RiccardosMFiles{$(MFILES)}\input{$(NAME).tex}"

Together with this main TeX file:

\documentclass[]{article}

\usepackage{matlab-prettifier}

\ExplSyntaxOn \NewDocumentCommand\showallmfiles{} { \exp_args:NNno \seq_set_split:Nnn \l_tmpa_seq { codice/ } \RiccardosMFiles \seq_pop_left:NN \l_tmpa_seq \l_tmpa_tl \seq_map_inline:Nn \l_tmpa_seq { \section{##1} \lstinputlisting[style=Matlab-editor]{codice/##1} } } \ExplSyntaxOff

\begin{document} \showallmfiles \end{document}

Skillmon
  • 60,462