1

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:

tellan@tellan1:~/Dokumente/kochen/buch/buch$ ls -1g *

Suppen_Eintoepfe:
insgesamt 12K
-rwxr-xr-x 1 tellan 539 Jul 21 2017 Chilieconcarne.txt
-rwxr-xr-x 1 tellan 1,3K Jul 21 2017 Waterzooi.txt
-rw-r--r-- 1 tellan 912 Nov 8 12:45 Wild_Consomme_Mit_Bratapfelkloesschen.txt

Teige_Und_Massen:
insgesamt 28K
-rw-r--r-- 1 tellan 848 Jul 22 2017 Biskuitteig.txt
-rw-r--r-- 1 tellan 633 Jul 22 2017 Brandteig.txt
-rw-r--r-- 1 tellan 226 Okt 5 15:29 Grundfarce.txt

This is my basic latex file:

\documentclass[a4paper,12pt,oneside,parskip,openany]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{geometry}
\usepackage{a4wide}
\usepackage{hyperref}
\usepackage[nonumber,contents,index]{cuisine}
\usepackage{nicefrac}
\usepackage{xpatch}
\usepackage[automark]{scrpage2}
\usepackage{ulem}
\usepackage{graphicx}
\usepackage{pdfpages}
\makeatletter
\xpatchcmd{\Displ@ySt@p}{\arabic{st@pnumber}}{}{}{}
\makeatother
\usepackage{makeidx}
\newcommand\tab[1][1cm]{\hspace*{#1}}
\renewcommand*{\recipetitlefont}{\sffamily}

\begin{document}

\title{Kochbuch}
\author{Ich}

\maketitle

\tableofcontents

\chapter{Nicht dein Kochbuch}
\newpage

\section{Vorspeisen Und Salate}
\input {Vorspeisen_Und_Salate/7-Layer-Dip.txt}
\input {Kuchen/Banana_Fudge_Brownies.txt}
\input {Kuchen/Bananen-Haferflocken-Muffins.txt}

\section{Soßen}
\input {Sossen/Asiatische_Bbq-Sosse.txt}
\input {Sossen/Barbecue-Sosse_Aus_Fertigprodukten.txt}
\input {Sossen/Currysosse_Nr_1.txt}

\section{Ect}
\input {Ect/Mengenangaben.txt}
\input {Ect/backformen.txt}
\input {Ect/umrechnungstabelle_us.txt}

\end{document}

per Hands is it working like a charme. I know it is not the cleanest command structure, but its working. Until know i can input the file via bash, but that doesnt include the directories as /section input.

#!/bin/bash
echo "" > sectionlist.tex
for sfile in `ls -1 */*.txt | cut -f 1 -d . | sort -d`
do
    echo "\input {$sfile.txt}" >> sectionlist.tex
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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.