Text FormattingBoxes and Font size

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
rspringuel
Posts: 19
Joined: Wed Apr 28, 2010 7:16 pm

Boxes and Font size

Post by rspringuel »

I'm writing a macro which is designed to allow the user to fill a box in a recursive fashion. I.e. if the box is empty, then the macro simply populates the box with the user designated material. If the box is not empty, then the new material is added to the old material on a new line (with alignment between the new and old material controlled by an optional argument). Spacing within the box is supposed to be controlled by some parameters which are set elsewhere, but I'm having some problems getting the spacing control right. There seems to be some extra vertical space being introduced.

Below you'll find my MWE, which has a stripped down version of the internal macro I'm writing. As you can see, there is extra space between the two lines of the material in the box when I use it (one example at normal text size, and another with [cmd]\scriptsize[/cmd] text because that introduces even more space).

In my attempts to work out what's going wrong, I tried stripping things down even further to produce the last two examples. These work, but I'm not sure what the difference between them and the macro is. As best I can tell, I've reproduced all of the boxing steps just with fixed values for the parameters and without recursive function calling. They clearly show that what I want to do is possible. How do I get my macro to produce what these later examples are showing is possible (though without the customizable parameters).

Note: While I don't believe it makes a difference here, the package which this macro appears in is targeted for LuaLaTeX and LuaTeX (it is compatible with both).

Code: Select all

% !TEX program = LuaLaTeX+se
\documentclass{report}

\makeatletter
\newbox\gre@box@temp@width%
\newdimen\gre@dimen@temp@three

\newdimen\gre@dimen@annotationseparation
\gre@dimen@annotationseparation = 0pt

\def\gre@widthof#1{%
	\setbox\gre@box@temp@width=\hbox{#1}%
	\global\gre@dimen@temp@three=\wd\gre@box@temp@width%
	\relax%
}%

\newbox \gre@box@annotation@old
\newbox \gre@box@annotation@add
\newbox \gre@box@annotation

\def\gre@annotation[#1]#2{%
	\ifx l#1\relax%
		\let\gre@rightfill\hfil%
		\let\gre@leftfill\relax%
	\else\ifx r#1\relax%
		\let\gre@rightfill\relax%
		\let\gre@leftfill\hfil%
	\else\ifx c#1\relax%
		\let\gre@rightfill\hfil%
		\let\gre@leftfill\hfil%
	\else%
		\gre@error{Unrecognzied alignment option for \protect\greannotation}%
	\fi\fi\fi%
	% first we determine the widest box
	\gre@widthof{#2}%
	\ifdim\gre@dimen@temp@three < \wd\gre@box@annotation\relax%
		\gre@dimen@temp@three = \wd\gre@box@annotation\relax%
	\fi%
	\setbox\gre@box@annotation@old = \hbox to \gre@dimen@temp@three{\gre@leftfill\copy\gre@box@annotation\gre@rightfill}%
	\setbox\gre@box@annotation@add = \hbox to \gre@dimen@temp@three{\gre@leftfill#2\gre@rightfill}%
	\ifvoid\gre@box@annotation%
		\setbox\gre@box@annotation = \hbox to \gre@dimen@temp@three{\vtop{{\offinterlineskip\vss\noindent\box\gre@box@annotation@add}}}%
	\else%
		\setbox\gre@box@annotation = \hbox to \gre@dimen@temp@three{\vtop spread \gre@dimen@annotationseparation\relax{{\offinterlineskip\noindent\box\gre@box@annotation@old\vss\noindent\box\gre@box@annotation@add}}}%
	\fi%
}%


\begin{document}

\gre@annotation[c]{Test}
\gre@annotation[c]{Also Test}

|\box\gre@box@annotation|

\bigskip
\noindent\rule{\textwidth}{0.4pt}
\bigskip

\gre@annotation[c]{\scriptsize Test}
\gre@annotation[c]{\scriptsize Also Test}

|\box\gre@box@annotation|

\bigskip
\noindent\rule{\textwidth}{0.4pt}
\bigskip

\newbox\original
\newbox\old
\newbox\intermediate
\newbox\new
\newbox\combine

\setbox\original = \hbox to 1in{\hfil Test\hfil}
\setbox\intermediate = \hbox to 1in{\vtop{{\offinterlineskip\vss\noindent\box\original}}}
\setbox\old = \hbox to 1in {\hfil\copy\intermediate\hfil}
\setbox\new = \hbox to 1in {\hfil Also Test\hfil}
\setbox\combine = \hbox to 1in {\vtop spread 0pt {\offinterlineskip\noindent\box\old\vss\noindent\box\new}}

|\box\combine|

\bigskip
\noindent\rule{\textwidth}{0.4pt}
\bigskip

\setbox\original = \hbox to 1in{\hfil\scriptsize Test\hfil}
\setbox\intermediate = \hbox to 1in{\vtop{{\offinterlineskip\vss\noindent\box\original}}}
\setbox\old = \hbox to 1in {\hfil\copy\intermediate\hfil}
\setbox\new = \hbox to 1in {\hfil \scriptsize Also Test\hfil}
\setbox\combine = \hbox to 1in {\vtop spread 0pt {\offinterlineskip\noindent\box\old\vss\noindent\box\new}}

|\box\combine|


\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

rspringuel
Posts: 19
Joined: Wed Apr 28, 2010 7:16 pm

Boxes and Font size

Post by rspringuel »

So, with some time to spare today for this problem, I decided to try rewriting the macro from scratch (instead of adapting the existing code) and was able to solve my problem. Below is a MWE which shows the macro working as expected. The new version of the macro doesn't contain quite so many embedded boxes, which makes me think that it was the process of embedding boxes within boxes that somewhere along the line led to the problem.

It just goes to show that sometimes you're better off starting from scratch rather than trying to adapt old code.

I still don't understand why the original code didn't work as intended though, so if some one has an explanation for that I'm interested in learning more.

Code: Select all

% !TEX program = LuaLaTeX+se
\documentclass{report}

\makeatletter
\newbox\gre@box@temp@width%
\newdimen\gre@dimen@temp@three

\newdimen\gre@dimen@annotationseparation
\gre@dimen@annotationseparation = 0pt

\def\gre@widthof#1{%
	\setbox\gre@box@temp@width=\hbox{#1}%
	\global\gre@dimen@temp@three=\wd\gre@box@temp@width%
	\relax%
}%

\newbox \gre@box@annotation@old
\newbox \gre@box@annotation@add
\newbox \gre@box@annotation

\def\gre@annotation[#1]#2{%
	\ifx l#1\relax%
		\let\gre@rightfill\hfil%
		\let\gre@leftfill\relax%
	\else\ifx r#1\relax%
		\let\gre@rightfill\relax%
		\let\gre@leftfill\hfil%
	\else\ifx c#1\relax%
		\let\gre@rightfill\hfil%
		\let\gre@leftfill\hfil%
	\else%
		\gre@error{Unrecognzied alignment option for \protect\greannotation}%
	\fi\fi\fi%
	% first we determine the widest box
	\gre@widthof{#2}%
	\ifdim\gre@dimen@temp@three < \wd\gre@box@annotation\relax%
		\gre@dimen@temp@three = \wd\gre@box@annotation\relax%
	\fi%
	\ifvoid\gre@box@annotation%
		\setbox\gre@box@annotation = \hbox to \gre@dimen@temp@three{\gre@leftfill#2\gre@rightfill}%
	\else%
		\setbox\gre@box@annotation@old = \hbox to \gre@dimen@temp@three{\gre@leftfill\box\gre@box@annotation\gre@rightfill}
		\setbox\gre@box@annotation@add = \hbox to \gre@dimen@temp@three{\gre@leftfill#2\gre@rightfill}%
		\setbox\gre@box@annotation = \vtop spread \gre@dimen@annotationseparation\relax {\offinterlineskip\box\gre@box@annotation@old\vss\box\gre@box@annotation@add}%
	\fi%
}%


\begin{document}

\gre@annotation[c]{Test}
\gre@annotation[c]{Also Test}

|\box\gre@box@annotation|

\bigskip
\noindent\rule{\textwidth}{0.4pt}
\bigskip

\gre@annotation[c]{\scriptsize Test}
\gre@annotation[c]{\scriptsize Also Test}

|\box\gre@box@annotation|

\end{document}
Post Reply