Skip to content

Commit 4a86cff

Browse files
committed
update images are now scaled and cropped inside LaTeX
1 parent 01daa5e commit 4a86cff

File tree

2 files changed

+160
-127
lines changed

2 files changed

+160
-127
lines changed

recipebook.cls

Lines changed: 146 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -19,183 +19,204 @@
1919
\RequirePackage{tocloft}
2020
\RequirePackage{etoolbox}
2121
\RequirePackage{fancyhdr}
22+
\RequirePackage{trimclip}
23+
\RequirePackage{tabularx}
2224

25+
% Define a custom command to set the document title
26+
% Usage: \settitle{Your Title} sets \title to "Your Title"
2327
\newcommand{\settitle}[1]{\renewcommand{\title}{#1}}
28+
29+
% Define a custom command to set the document author
30+
% Usage: \setauthor{Your Name} sets \author to "Your Name"
2431
\newcommand{\setauthor}[1]{\renewcommand{\author}{#1}}
2532

26-
\settitle{My Recipe Book}
27-
\setauthor{Niklas Hoefflin}
33+
% Set the actual title and author of the recipe book
34+
\settitle{My Recipe Book} % Title metadata and reference
35+
\setauthor{Niklas Hoefflin} % Author metadata and reference
36+
37+
% Load the hyperref package to enable hyperlinks and set PDF metadata
2838
\RequirePackage[
29-
colorlinks=false,
30-
linkcolor=blue,
31-
pdfauthor={\author},
32-
pdftitle={\title},
33-
pdfsubject={A collection of delicious recipes},
34-
pdfkeywords={cooking, recipes, cookbook},
35-
pdfproducer={LaTeX},
36-
pdfcreator={pdflatex}
39+
hidelinks, % Hide link borders and colors (clean look)
40+
pdfauthor={\author}, % Set PDF metadata author from \author
41+
pdftitle={\title}, % Set PDF metadata title from \title
42+
pdfsubject={A collection of delicious recipes}, % Subject metadata
43+
pdfkeywords={cooking, recipes, cookbook}, % Keywords metadata
44+
pdfproducer={XeLaTeX}, % Tool used to create PDF
45+
pdfcreator={XeLaTeX} % Engine used to compile the document
3746
]{hyperref}
3847

3948
% Fonts
4049
\setmainfont[
41-
Path=./fonts/,
42-
UprightFont = *-Regular.ttf,
43-
BoldFont = *-Bold.ttf,
44-
ItalicFont = *-Italic.ttf,
45-
BoldItalicFont = *-BoldItalic.ttf
50+
Path=./fonts/,
51+
UprightFont = *-Regular.ttf,
52+
BoldFont = *-Bold.ttf,
53+
ItalicFont = *-Italic.ttf,
54+
BoldItalicFont = *-BoldItalic.ttf
4655
]{PublicSans}
4756

4857
\newfontfamily\fontseasons[
49-
Path=./fonts/,
50-
UprightFont = *Regular.ttf,
51-
BoldFont = *Bold.ttf,
52-
ItalicFont = *Italic.ttf,
53-
BoldItalicFont = *BoldItalic.ttf
58+
Path=./fonts/,
59+
UprightFont = *Regular.ttf,
60+
BoldFont = *Bold.ttf,
61+
ItalicFont = *Italic.ttf,
62+
BoldItalicFont = *BoldItalic.ttf
5463
]{TheSeasons}
5564

5665
\newfontfamily\fontseasonslight[
57-
Path=./fonts/,
58-
UprightFont = *Light.ttf,
59-
ItalicFont = *LightItalic.ttf,
60-
BoldFont = *Light.ttf
66+
Path=./fonts/,
67+
UprightFont = *Light.ttf,
68+
ItalicFont = *LightItalic.ttf,
69+
BoldFont = *Light.ttf
6170
]{TheSeasons}
6271

6372
% Layout
64-
\newlength{\picmargin}
65-
\setlength{\picmargin}{15pt}
66-
\newlength{\picwidth}
67-
\setlength{\picwidth}{\paperwidth - 2\picmargin}
68-
6973
\geometry{
70-
top=0pt,
71-
bottom=2cm,
72-
left=\picmargin,
73-
right=\picmargin
74+
top=0cm,
75+
bottom=2cm,
76+
left=0cm,
77+
right=0cm
7478
}
7579

76-
7780
\pagestyle{fancy}
7881
\fancyhf{}
7982

8083
\fancyfoot[C]{\fontsize{10}{12}\selectfont \thepage}
8184

82-
\renewcommand{\headrulewidth}{0pt}
83-
\renewcommand{\footrulewidth}{0pt}
84-
85-
\fancypagestyle{plain}{
86-
\fancyhf{}
87-
\fancyfoot[C]{\fontsize{10}{12}\selectfont \thepage}
88-
\renewcommand{\headrulewidth}{0pt}
89-
\renewcommand{\footrulewidth}{0pt}
90-
}
91-
85+
% Create a section that appears in the Table of Contents, but not visibly in the document
9286
\newcommand\invisiblesection[1]{%
93-
\refstepcounter{section}%
94-
\addcontentsline{toc}{section}{\protect\numberline{\thesection}#1}%
95-
\sectionmark{#1}}
87+
\refstepcounter{section}%
88+
\addcontentsline{toc}{section}{\protect\numberline{\thesection}#1}%
89+
\sectionmark{#1}}
9690

9791
\addto\captionsenglish{%
98-
\renewcommand{\contentsname}{\fontsize{28}{32}\selectfont\fontseasons{Table of Contents}}
92+
\renewcommand{\contentsname}{\begin{center}\fontsize{28}{32}\selectfont\fontseasons{Table of Contents}\end{center}}
9993
}
10094

101-
\renewcommand{\cftsecfont}{}
102-
\renewcommand{\cftsecpagefont}{\large}
95+
\renewcommand{\cftsecfont}{\fontsize{16}{20}\selectfont\fontseasons}
96+
\renewcommand{\cftsecpagefont}{\fontsize{16}{20}\selectfont}
10397

10498
\makeatletter
105-
% Redefine section ToC entry to have dots
10699
\renewcommand{\l@section}[2]{%
107-
\@dottedtocline{1}{2pt}{2em}{#1}{#2}%
100+
\addpenalty{\@secpenalty}%
101+
\addvspace{1em}%
102+
\begingroup
103+
\parindent=0pt
104+
\centering
105+
\fontsize{14}{16}\selectfont\fontseasons
106+
\makebox[\textwidth][c]{%
107+
\makebox[0.7\textwidth][c]{#1\hfill#2}%
108+
}%
109+
110+
\par
111+
\endgroup
108112
}
109113
\makeatother
110114

111-
% Colors
115+
116+
117+
118+
119+
120+
% Color used for the ingredients box background
112121
\definecolor{bgIngredients}{HTML}{F8EDE9}
113-
\definecolor{textColor}{HTML}{000000}
114122

115-
% Macros
116-
\newcommand{\recipeTitle}[1]{
117-
\vspace{0.2cm}
118-
\parbox{\picwidth}{
119-
\centering
120-
\fontsize{28}{32}\selectfont\fontseasons{\textbf{#1}}}
123+
% Command to format and display the recipe title nicely
124+
\newcommand{\recipeTitle}[1]{%
125+
\vspace{0.2cm}
126+
\noindent
127+
\begin{center}
128+
{\fontsize{28}{32}\selectfont\fontseasons\textbf{#1}}
129+
\end{center}
130+
\vspace{0.1cm}
121131
}
122132

123-
% === Recipe metadata ===
124-
\newcommand{\recipeName}{}
125-
\newcommand{\servings}{}
126-
\newcommand{\prepTime}{}
127-
\newcommand{\cookTime}{}
128-
\newcommand{\recipeImage}{}
133+
% Define placeholders for recipe metadata, initially empty
134+
\newcommand{\recipeName}{} % The title of the recipe
135+
\newcommand{\servings}{} % Number of servings
136+
\newcommand{\prepTime}{} % Preparation time
137+
\newcommand{\cookTime}{} % Cooking time
138+
\newcommand{\recipeImage}{} % Filename/path of the recipe image
129139

140+
% Command to set all recipe metadata in one call
141+
% Usage: \setRecipeMeta{Title}{Servings}{Prep Time}{Cook Time}{Image Filename}
130142
\newcommand{\setRecipeMeta}[5]{%
131-
\renewcommand{\recipeName}{#1}%
132-
\renewcommand{\servings}{#2}%
133-
\renewcommand{\prepTime}{#3}%
134-
\renewcommand{\cookTime}{#4}%
135-
\renewcommand{\recipeImage}{#5}%
143+
\renewcommand{\recipeName}{#1}% Set the recipe title
144+
\renewcommand{\servings}{#2}% Set the number of servings
145+
\renewcommand{\prepTime}{#3}% Set the preparation time
146+
\renewcommand{\cookTime}{#4}% Set the cooking time
147+
\renewcommand{\recipeImage}{#5}% Set the path to the recipe image
136148
}
137149

150+
151+
% Macro to define a single ingredient item in the ingredients list
152+
\newcommand{\ingredient}[1]{\item{#1}}
153+
% Macro to define a single cooking step in the steps list
154+
\newcommand{\step}[1]{\item{#1}}
155+
156+
157+
% Trick to auto scale recipe images
158+
% https://tex.stackexchange.com/questions/547621/crop-an-image-to-exact-measurements
159+
\newcommand\picdims[4][]{%
160+
\setbox0=\hbox{\includegraphics[#1]{#4}}%
161+
\clipbox{.5\dimexpr\wd0-#2\relax{} %
162+
.5\dimexpr\ht0-#3\relax{} %
163+
.5\dimexpr\wd0-#2\relax{} %
164+
.5\dimexpr\ht0-#3\relax}{\includegraphics[#1]{#4}}}
165+
138166
% Recipe environment
139167
\newenvironment{recipe}{
140-
\clearpage
141-
\pagestyle{fancy}
142-
\phantomsection
143-
\refstepcounter{section}
144-
\addcontentsline{toc}{section}
145-
{\protect\numberline{\thesection} \recipeName}
146-
\begin{center}
147-
\makebox[\textwidth]{\includegraphics[width=\paperwidth]{\recipeImage}}
148-
\end{center}
149-
150-
\vspace{0.6cm}
151-
\hspace{-\picmargin}
152-
\recipeTitle{\recipeName}
153-
154-
\begin{center}
155-
\begin{tabular}{c c c}
156-
\fontsize{12}{16}\selectfont{SERVINGS: \servings} \hspace{2.5cm} &
157-
\fontsize{12}{16}\selectfont{PREPPING TIME: \prepTime} &
158-
\hspace{.5cm} \fontsize{12}{16}\selectfont{COOKING TIME: \cookTime}
159-
\end{tabular}
160-
\vspace{2pt}
161-
\begin{tikzpicture}
162-
\draw[very thick] (4,0) -- (\linewidth+15pt,0);
163-
\end{tikzpicture}
164-
\end{center}
165-
166-
\vspace{1cm} % optional vertical spacing before
167-
168+
\clearpage
169+
\pagestyle{fancy}
170+
\phantomsection
171+
\refstepcounter{section}
172+
\addcontentsline{toc}{section}{\recipeName}
173+
\noindent\makebox[\paperwidth][l]{%
174+
\picdims[width=\paperwidth]{\paperwidth}{225.75pt}{\recipeImage}%
175+
}
176+
\vspace{0.2cm}
177+
\recipeTitle{\recipeName}
178+
\begin{center}
179+
\begin{minipage}{16cm}
180+
\begin{center}
181+
\begin{tabularx}{15cm}{@{}lXcXr@{}}
182+
\fontsize{12}{16}\selectfont SERVINGS: \servings &
183+
&
184+
\fontsize{12}{16}\selectfont PREPPING TIME: \prepTime &
185+
&
186+
\fontsize{12}{16}\selectfont COOKING TIME: \cookTime
187+
\end{tabularx}
188+
\end{center}
189+
\vspace{-10pt}
190+
\begin{tikzpicture}
191+
\draw[very thick] (0,0) -- (16cm,0);
192+
\end{tikzpicture}
193+
\end{minipage}
194+
\end{center}
168195
}
169196

170197
% Ingredients environment
171-
172198
\newenvironment{ingredients}{
173-
\begin{tikzpicture}[remember picture, overlay]
174-
\node[fill=bgIngredients, opacity=1.0, anchor=north west, minimum width=6cm, minimum height=15cm, inner sep=0pt]
175-
at ([xshift=50pt,yshift=-345pt]current page.north west) {};
176-
\end{tikzpicture}%
177-
\hspace{35pt}%
178-
\begin{minipage}[t]{0.25\textwidth}
179-
\section*{{\fontsize{22}{26}\selectfont\fontseasonslight Ingredients}}
180-
\begin{itemize}[label={}, itemsep=4pt, leftmargin=0pt]
181-
}{
182-
\end{itemize}
183-
\end{minipage}%
199+
\vspace{1cm}
200+
\begin{tikzpicture}[remember picture, overlay]
201+
\node[fill=bgIngredients, opacity=1.0, anchor=north west, minimum width=6.5cm, minimum height=15cm, inner sep=0pt]
202+
at ([xshift=50pt,yshift=-350pt]current page.north west) {};
203+
\end{tikzpicture}%
204+
\hspace{70pt}%
205+
\begin{minipage}[t]{0.25\textwidth}
206+
\section*{{\fontsize{22}{26}\selectfont\fontseasonslight Ingredients}}
207+
\begin{itemize}[label={}, itemsep=4pt, leftmargin=0pt]
208+
}{
209+
\end{itemize}
210+
\end{minipage}%
184211
}
185212

186-
187213
% Steps environment
188214
\newenvironment{steps}{
189-
\hspace{35pt}%
190-
\begin{minipage}[t]{0.45\textwidth}
191-
\section*{{\fontsize{22}{26}\selectfont\fontseasonslight Instructions}}
192-
\begin{enumerate}[itemsep=4pt, leftmargin=13pt]
193-
}{%
194-
\end{enumerate}
195-
\end{minipage}
196-
}
197-
198-
199-
\newcommand{\ingredient}[1]{\item \textcolor{textColor}{#1}}
200-
201-
\newcommand{\step}[1]{\item \textcolor{textColor}{#1}}
215+
\hspace{34pt}%
216+
\begin{minipage}[t]{0.45\textwidth}
217+
\section*{{\fontsize{22}{26}\selectfont\fontseasonslight Instructions}}
218+
\begin{enumerate}[itemsep=4pt, leftmargin=13pt]
219+
}{%
220+
\end{enumerate}
221+
\end{minipage}
222+
}

toc.tex

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1-
\vspace*{2cm}
1+
% Define new geometry just for the TOC
2+
\newgeometry{
3+
top=2cm,
4+
bottom=2cm,
5+
left=2cm,
6+
right=2cm
7+
}
28
\tableofcontents
3-
\newpage
9+
% hides page number on the ToC page
10+
\thispagestyle{empty}
11+
\newpage
12+
% This is used to restore margins so that the images in the recipes don't get shifted.
13+
\restoregeometry
14+
% Start normal page numbering from 1
15+
\pagenumbering{arabic}

0 commit comments

Comments
 (0)