Page LayoutControl positioning of elements

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
petersfreeman
Posts: 19
Joined: Wed Apr 28, 2021 9:40 am

Control positioning of elements

Post by petersfreeman »

I'm creating a simple cookbook for my wife and I to use in our kitchen. A typical page output looks like this:

Image

The problem I'm having is to get consistency in the vertical positioning of elements. The order in which I apply elements is:
  1. Recipe title
  2. Created Recipe indicator
  3. Tested Recipe indicator
  4. Prep/Servings/Rating_Stars/Cook/Temp line
The way I am going about it is to:
  1. Output the title
  2. If a "heart" needs to be outputted, move the "cursor" back up to the middle of the title band (\vspace-20pt})
  3. Output the "heart" symbol
  4. Move the "cursor" back down to where I think it came from. (I'm guessing)
  5. If a "check" needs to be outputted, move the "cursor" back up to the middle of the title band (\vspace{-23pt})
  6. Output the "check" symbol after moving it across to the right hand side (\hfill) with a bit of space beyond it (\hspace{20pt}.
  7. Move the "cursor" back down to some default position
  8. output the Prep/Servings/Rating_Stars/Cook/Temp line
  9. Now output the ingredients and instructions etc...
My ignorance has led me to this solution. I know it is a bit of a kluge. I suspect I should be using minipages or boxes or some other process to contain things. But I don't have enough LaTeX experience to manage that.

There are four circumstances that could occur:
  1. Both "heart" and "check" appear with the title in the title band
  2. Only the "heart" appears with the title in the title band
  3. Only the "check" appears with the title in the title band
  4. Only the title is in the title band
What happens is the space between the greyed title band and the "Prep/Servings/Rating_Stars/Cook/Temp" line varies. I want the "Prep/Servings/Rating_Stars/Cook/Temp" line to always have a consistent amount of vertical space between it and the bottom of the title band.

How should I best go about this. Just give me the direction in which I should examine/research/learn and I'll figure it out.

Thanks,

Peter

The (partial) code to produce this piece is:

Code: Select all

		\newpage
%===============================================================================================
		\indexedsubsection{Italian Bread}
%===============================================================================================

		% Put in the Created Recipe indicator.
		\raggedright
		\vspace{-20pt}
		\hspace{20pt}\ding{164}
		\vspace{10pt}

		% Put in the Tested Recipe indicator.
		\raggedright
		\vspace{-23pt}
		\hfill\ding{52}\hspace{20pt}
		\vspace{13pt}

		% preparation time, servings, 3, and 40 time and oven temperature
		\vspace{-10pt}
		\begin{center}
			\begin{tabularx}{\textwidth}{ >{\raggedright\arraybackslash}X c >{\raggedleft\arraybackslash}X }
				{\small \textsc{Prep:} 40 min \textsc{Make:} 1 loaf} & {\large \ding{72}\ding{72}\ding{72}\ding{73}\ding{73}} & {\small \textsc{Cook:} 40 min @ 400°F}
			\end{tabularx}
		\end{center}
		\vspace{-10pt}

		% Middle band: Ingredients (two columns)
		\vspace{-\topsep}
		\begin{multicols}{2}
			\raggedright

			\begin{itemize}
				\item 1  cup  warm  water 
				\item 1  egg 
				\item 2  tbsps  olive oil 
				\item 2  tbsps  honey 
				\item 500  grams  flour 
			\end{itemize}
			\columnbreak
			\begin{itemize}
				\item 1½  tsps  salt 
				\item 1  tbsp  active dry  yeast 
				\item ⅓  cup  Italian  herb mix 
				\item ⅓  cup  tomatoes  (sun dried, chopped)
				\item ⅓  cup  olives  (chopped)
			\end{itemize}
		\end{multicols}
etc...etc...

The code for \indexedsubsection{Italian Bread} is pretty straightforward:

Code: Select all

\usepackage[]{imakeidx}				% makeidx: Standard LaTeX package for creating indexes
\usepackage[]{tcolorbox}			% tcolorbox: Coloured boxes, for LaTeX examples and theorems, etc

\makeindex

% Define custom gray colors
\definecolor{mylightgray}{RGB}{220, 220, 220} % RGB example

% Define the new tcolorbox environment with a custom gray color
\newtcolorbox{mysectionbox}[1][]{
	colback=mylightgray, % Use the custom color
	colframe=mylightgray,
	width=\textwidth,
	boxrule=0pt,
	left=0pt,
	right=0pt,
	top=10pt, % Adjust this value for the top padding
	bottom=10pt, % Adjust this value for the bottom padding
	boxsep=0pt,
	before skip=0pt,
	after skip=0pt,
	parbox=false,
	valign=center,
	fontupper=\bfseries\large\centering, % Center the text
	#1
}

% Redefine \subsection to use the custom tcolorbox environment and add to ToC and index
\newcommand{\indexedsubsection}[1]{%
	\vspace{0pt} % Adjust this value for white space above
	\begin{mysectionbox}
		\textbf{\large #1}
	\end{mysectionbox}
	\addcontentsline{toc}{subsection}{#1} % Add to table of contents
	\index{#1} % Add to index
	\vspace{0pt} % Adjust this value for white space below
}
:

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
petersfreeman
Posts: 19
Joined: Wed Apr 28, 2021 9:40 am

Control positioning of elements

Post by petersfreeman »

I have some success with this code using a tabularx environment to reposition my two indicators:

Code: Select all

\vspace{-20pt}
\begin{tabularx}{\textwidth}{p{0.43\textwidth}p{0.44\textwidth}}
	\hspace{0pt}\ding{164} & \hfill\ding{52}\hspace{0pt}
\end{tabularx}
\vspace{20pt}
I still have to test it out through the four possible situations.
petersfreeman
Posts: 19
Joined: Wed Apr 28, 2021 9:40 am

Control positioning of elements

Post by petersfreeman »

I discovered the reason for the varying vertical between the bottom of the grey band and the "Prep/Servings/Rating_Stars/Cook/Temp" line. If the recipe title has descenders, i.e, g, j, p, q, and y, then the band adjusts by widening to accommodate them. The top of the band stays exactly where it is for all titles, but the bottom of the grey band moves down to accommodate the descenders. I don't need to worry about ascenders as all titles have capital letters in them which fixes the top.

It seems that I need to adjust my grey band so it doesn't change to accommodate the descenders so I have to look at this code:

Code: Select all

\usepackage[]{imakeidx}				% makeidx: Standard LaTeX package for creating indexes
\usepackage[]{tcolorbox}			% tcolorbox: Coloured boxes, for LaTeX examples and theorems, etc

\makeindex

% Define custom gray colors
\definecolor{mylightgray}{RGB}{220, 220, 220} % RGB example

% Define the new tcolorbox environment with a custom gray color
\newtcolorbox{mysectionbox}[1][]{
	colback=mylightgray, % Use the custom color
	colframe=mylightgray,
	width=\textwidth,
	boxrule=0pt,
	left=0pt,
	right=0pt,
	top=10pt, % Adjust this value for the top padding
	bottom=10pt, % Adjust this value for the bottom padding
	boxsep=0pt,
	before skip=0pt,
	after skip=0pt,
	parbox=false,
	valign=center,
	fontupper=\bfseries\large\centering, % Center the text
	#1
}

% Redefine \subsection to use the custom tcolorbox environment and add to ToC and index
\newcommand{\indexedsubsection}[1]{%
	\vspace{0pt} % Adjust this value for white space above
	\begin{mysectionbox}
		\textbf{\large #1}
	\end{mysectionbox}
	\addcontentsline{toc}{subsection}{#1} % Add to table of contents
	\index{#1} % Add to index
	\vspace{0pt} % Adjust this value for white space below
}
Somehow, I need to change:

Code: Select all

	top=10pt, % Adjust this value for the top padding
	bottom=10pt, % Adjust this value for the bottom padding
in a way that fixes the width of my grey band. Back to the drawing board.

Hmm...

Maybe I'll separate the drawing of the band from the title:

Code: Select all

\newcommand{\indexedsubsection}[1]{%
	\vspace{0pt} % Adjust this value for white space above
	\textbf{\large #1}
	\addcontentsline{toc}{subsection}{#1} % Add to table of contents
	\index{#1} % Add to index
	\vspace{0pt} % Adjust this value for white space below
}
That will give me just the title. Now I have to figure out how to do a separate grey box that is \textwidth wide from the left text margin to the right text margin and has a fixed vertical height. Putting my Gyro Gearloose cap on...where's my little helper when I need him...

Peter
petersfreeman
Posts: 19
Joined: Wed Apr 28, 2021 9:40 am

Control positioning of elements

Post by petersfreeman »

Got it!

I needed to read the tcolorbox manual to figure it out. Here is the code to set up the box to take three parameters:
  1. Left indicator (heart)
  2. Recipe title
  3. Right indicator (check)

Code: Select all

\makeindex

% Define custom gray colors
\definecolor{mylightgray}{RGB}{220, 220, 220} % RGB example

\newtcolorbox{titleband}{colback=mylightgray, colframe=mylightgray,arc=3mm, width=\textwidth, height=1.1cm, halign=flush center}

% Redefine \subsection to use the custom tcolorbox environment and add to ToC and index
\newcommand{\indexedsubsection}[3]{%
	\begin{titleband}
		\begin{tabularx}{\textwidth}{ >{\raggedright\arraybackslash}X c >{\raggedleft\arraybackslash}X }
			#1 & \textbf{\large #2} & #3
		\end{tabularx}
	\end{titleband}
	\addcontentsline{toc}{subsection}{#2} % Add to table of contents
	\index{#2} % Add to index
}
And here is the code in the mainmatter:

Code: Select all

\indexedsubsection{\ding{164}}{Italian Bread}{\ding{52}}
It looks like this:
Image

I still need to check all of the recipes to make sure the vertical spacing is consistent.

I checked and the vertical space is consistent in all recipes that easily fit on the page (there is still room for more lines of text). However, if the page is completely filled or the recipe requires more than one page, it pushes my "prep/servings/rating_star/cook/temp" line up a bit.

I used the option \documentclass[raggedbottom]{book} but it didn't seem to have an effect. It may be related to a "rubber" amount that is being applied to either the "prep/servings/rating_star/cook/temp" line or the text in the title band. I need to figure out how to set that "rubber" amount to zero.

Peter.
Post Reply