Text FormattingHeight of Multiple Lines of Text

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
PSNYLaTexUser
Posts: 4
Joined: Fri Dec 31, 2010 4:32 am

Height of Multiple Lines of Text

Post by PSNYLaTexUser »

I am trying to use the following commands to set a length variable that measures the height of specific text:

Code: Select all

\newlength{\mytextheight}
\newsavebox{\mytext}
\savebox{\mytext}{ENTER TEXT HERE}
\settoheight{\mytextheight}{\usebox{\mytext}}
These commands work perfectly fine when there is a single line of text, but fail in at least two ways with multiple lines.

First, if multiple lines or paragraphs are used in the "ENTER TEXT HERE" field above, the length returns is only the height of the first line.

Second, when I use something like a display equation or a list environment within the text, I get error messages.

I would appreciate someone helping me to determine how to either modify the above commands so that they work with multiple lines of text, equations, etc. or suggest an alternative.

It isn't important, but in case it helps what I am trying to do is write an exam with solutions that will only be displayed in some cases. I want the empty space between questions to match the height of the space that will be taken up by the solution. I am using the exam package for this and the solution environment, which takes an optional parameter that determines the amount of white space shown when the solution is not shown. I am trying to set that parameter so it matches the height of the actual solution.

Here is a minimal working example showing the results I get.

Code: Select all

% MWE of Problem With \settoheight

% Use this for standard output
\documentclass{article}

\newlength{\mytextheight}
\newsavebox{\mytext}

\newcommand{\TestHeight}[1]
	{
		\savebox{\mytext}{#1}
		\settoheight{\mytextheight}{\usebox{\mytext}}
		{#1}

		Height of Text: \the\mytextheight\\
	}

\begin{document}
\TestHeight
{This is sample text.}


\TestHeight
{\Large This is larger text.}


\TestHeight
{
This is sample text with multiple paragraphs.


This is the second paragraph.

Notice that the height is the same as when there was only one line.

}

\TestHeight
{
This is sample text with an equation: $e = mc^2$
}

% The following version results in an error

%\TestHeight
%{
%This is sample text with a displayed equation: 
%\[e = mc^2\]
%}

\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

sitex
Posts: 70
Joined: Sat May 09, 2009 12:37 pm

Height of Multiple Lines of Text

Post by sitex »

Hello,

May I suggest you try Don Story's exerquiz package which is part of his eDucation bundle http://www.math.uakron.edu/~dpstory/webeq.html. The basic structure of a question is

Code: Select all

\begin{exerise}
Statement of a question.
\begin{solution}[2in] %optional vertical skip
Solution to the problem
\end{solution
\end{exercise}
When the nosolutions option is chosen the formatted document contains the question and then 2 inches of space in which to write the solution. When the solutions option is chosen the typeset file contains the question followed by the solution.

You are also able to add point values to questions; the typeset document will contain these point values. You can elect to display the sum of the number of points in an exam.

Tom
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Height of Multiple Lines of Text

Post by frabjous »

So consider sitex's answer, but with regard to what you're looking for, consider the \phantom command, which leaves the exact blank space that would otherwise be occupied by its contents. There's also \vphantom which will set the height of the current box to at least the height of what goes in the \vphantom. Indeed, you might define a command:

As for the problems you were having, \settoheight, \savebox, and for that matter, \phantom and \vphantom too, put their contents in LR-mode, not paragraph mode (See here), so if they are going to be multiple lines, you'll need to put a parbox inside them.

Code: Select all

\newcommand{\solution}[1]{\par\smallskip\noindent\phantom{\parbox{\textwidth}{%
#1
}}\par\smallskip}
Then if you want to print it with the solutions, just leave out the \phantom part.

That's not perfect. You can do line breaks, but you still can't do paragraph breaks inside there. Why not? Well, keep in mind that the space between paragraphs in LaTeX stretches to fill the available whitespace, and so it doesn't actually know the height of multiple paragraphs until the entire page has been typeset, and so that creates a problem in cases like this.

You could just simulate paragraph breaks with linebreaks and ident commands...

But also... if you're only printing these, and not distributing them electronically, you could consider doing nothing other than switch the text color to white for the passage. (If you distribute them electronically, of course you could still reveal the answers by selecting the text, so that wouldn't work.)
Post Reply