Code: Select all
\newlength{\mytextheight}
\newsavebox{\mytext}
\savebox{\mytext}{ENTER TEXT HERE}
\settoheight{\mytextheight}{\usebox{\mytext}}
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}