# Howto add batch \section and \input in a Latex document

I'm working on a Cook Book for my recipes. I use the cuisine package and all my recipes are in seperated files in seperated directories. To include the files in latex per hands is not the problem. But i like to automate it. Because the fies are a "work in progress" and adding or changing often. I found a bash script here: https://tex.stackexchange.com/questions/298792/sort-sections-according-to-alphabetical-order/299600#299600. But this doesn't include directories. Here is an example from my file structure:

done


Thanks to Masroor for the script. I'm working with sed syntaxes as well. But without any success until now. I have not the best programming skills.

I guess this isn't really a (La)TeX question, but does this script do what you're asking for?

The first part just creates the directories/files you mentioned (if they don't already exist, otherwise it does nothing). The second part creates a tex-file that contains \section{<dirname with spaces instead of underscores>} for every directory and \input{<filename>} for every text file in these directories.

#!/bin/bash

########################################
## Remove this for the final version: ##
########################################
mkdir -p "Suppen_Eintoepfe" "Teige_und_Massen"
touch "Suppen_Eintoepfe/Chilieconcarne.txt" "Suppen_Eintoepfe/Waterzooi.txt" "Suppen_Eintoepfe/Wild_Consomme_Mit_Bratapfelkloesschen.txt"
touch "Teige_und_Massen/Biskuitteig.txt" "Teige_und_Massen/Brandteig.txt" "Grundfarce.txt"
########################################

echo "%% This file was automatically generated by $(basename "$0")" > sectionlist.tex
find . -mindepth 1 -maxdepth 1 -type d | cut -c 3- | sort -df | while read -r sdir; do
txtfilefound=false #*
find "$sdir" -maxdepth 1 -type f -iname '*.txt' | cut -f 1 -d . | sort -df | while read -r sfile; do if [ "$txtfilefound" = false ]; then #*
echo $'\n'"\\section{${sdir//_/ }}" >> sectionlist.tex #*
txtfilefound=true #*
fi #*
echo "\\input{\$sfile.txt}" >> sectionlist.tex
done
done


After running this, the contents of sectionlist.tex will be:

%% This file was automatically generated by makesectionlist.sh

\section{Suppen Eintoepfe}
\input{Suppen_Eintoepfe/Chilieconcarne.txt}
\input{Suppen_Eintoepfe/Waterzooi.txt}
\input{Suppen_Eintoepfe/Wild_Consomme_Mit_Bratapfelkloesschen.txt}

\section{Teige und Massen}
\input{Teige_und_Massen/Biskuitteig.txt}
\input{Teige_und_Massen/Brandteig.txt}


This might not work that well if you've got section titles with special characters though (which seems likely since the recipe names are in German). If that is a problem you could consider creating a file called 0title.txt in every directory containing just \section{<Section title>}, with <Section title> replaced by whatever the title for that section should be. This file would be sorted before the files that start with a letter. If you do this you should remove all the lines marked by #* above.

Note: sort -d is case sensitive, which means that it sorts all capital letters before all lowercase letters. Since this is probably not what you want, I've used sort -df instead.