Common (recipes that we both use)
Mary (recipes that only she likes)
Peter (recipes that only I like)
To define the book structure, I’m starting simply using an Excel spreadsheet to layout the structure and save it as a CSV file as shown below (The first line is the header):
"Part","Chapter","Section"
"Common","Breakfast","Banana Pancakes"
"Common","Breakfast","Tortilla de Patatas"
"Common","Candy","Chocolate Salami"
"Common","Cookies","Cranberry Walnut Oatmeal Cookies"
"Common","Cookies","Gingersnap Cookies"
"Mary","Salads","Caesar Salad"
"Mary","Salads","Quinoa and Black Beans Salad"
"Peter","Muffins","Bran Muffins"
"Peter","Muffins","Cornmeal Muffins"
"Peter","Soups","Thai Coconut Chicken Soup"
Using datatool, I want output similar to as if I hard coded this way:
Code: Select all
\documentclass[]{book}
\begin{document}
\part{Common}
\chapter{Breakfast}
\section{ Banana Pancakes}
\section{Tortilla de Patatas}
\chapter{Candy}
\section{Chocolate Salami}
\chapter{Cookies}
\section{Cranberry Walnut Oatmeal Cookies}
\section{Gingersnap Cookies}
\part{Mary}
\chapter{Salads}
\section{Caesar Salad}
\section{Quinoa and Black Beans Salad}
\part{Peter}
\chapter{Muffins}
\section{Bran Muffins}
\section{Cornmeal Muffins}
\chapter{Soups}
\section{Thai Coconut Chicken Soup}
\end{document}
Code: Select all
\documentclass[]{book}
\usepackage[]{datatool}
% Load the CSV file
\DTLloaddb{recipes}{Recipes.csv}
\begin{document}
% Initialize variables to store previous values
\newcommand{\prevpart}{}
\newcommand{\prevchapter}{}
\newcommand{\prevsection}{}
% Iterate through the database and insert the data into the corresponding LaTeX commands
\DTLforeach{recipes}{\partname=Part,\chaptername=Chapter,\sectionname=Section}{%
% Insert \part if different from the previous one
\ifx\partname\prevpart\else
\part*{\partname}
\renewcommand{\prevpart}{\partname}
\fi
% Insert \chapter if different from the previous one
\ifx\chaptername\prevchapter\else
\chapter*{\chaptername}
\renewcommand{\prevchapter}{\chaptername}
\fi
% Insert \section if different from the previous one
\ifx\sectionname\prevsection\else
\section*{\sectionname}
\renewcommand{\prevsection}{\sectionname}
\fi
}
\end{document}
Peter