# settototalheight issue with empty lines

I am using \settototalheight to calculate the height of the text in order to use needspace correctly.

This is the code i am using :

\documentclass[12pt]{article}

\usepackage{graphicx}
\usepackage[parfill]{parskip}
\usepackage[none]{hyphenat}
\sloppy
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{framed}
\usepackage{tikz}
\usepackage{array,ragged2e, calc}
\usepackage{enumitem}
\usepackage{lipsum}
\usepackage{mdframed}
\usepackage{needspace}
\usepackage{nolbreaks}

% ------------------------------------------------------------------------------------
% Fonts
% ------------------------------------------------------------------------------------
\usepackage{xunicode}
\usepackage{fontspec}
\usepackage{xltxtra}
\usepackage{txfonts}
% ------------------------------------------------------------------------------------

\definecolor{shadecolor}{RGB}{217,217,217}  % Color used for highlighting
\definecolor{light-blue}{RGB}{0,175,236}    % Color for the footer

% ------------------------------------------------------------------------------------
% Header and footer management.
% ------------------------------------------------------------------------------------
\usepackage{fancyhdr}
\pagestyle{fancy}

% clear any old style settings
\fancyhf{}

\setmainfont{Calibri}

% ------------------------------------------------------------------------------------
% Defining the section style
% ------------------------------------------------------------------------------------

\renewcommand{\footrulewidth}{1pt}
\renewcommand{\footrule}{{\color{light-blue}%
\vskip-\footruleskip\vskip-\footrulewidth

% ------------------------------------------------------------------------------------

\usepackage[left=0.75in,top=1.5in,right=0.75in,bottom=1in]{geometry} % Document margins
\usepackage{titlesec}

\newlength{\rectanglelength}
\setlength{\rectanglelength}{\textwidth}

\newlength{\foo}

\begin{document}
\lipsum[1]

\settototalheight{\foo}{\parbox[t]{\linewidth}{\begin{minipage}[t]{\linewidth} \begin{tabular}{@{}>{\RaggedRight}p{11cm}>{\RaggedLeft}p{6.35cm}@{}}\fontsize{13}{6}\selectfont{LaTeX}&\textbf{2011 to 2012}\end{tabular}\\\begin{tabular}{@{}>{\RaggedRight}p{11cm}>{\RaggedLeft}p{6.35cm}@{}}\textbf{Lead Programmer}&\textbf{}\end{tabular}%
\vspace{3pt}\begin{itemize}[parsep=0pt, topsep=0pt, itemsep=0pt, leftmargin=2ex]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\end{itemize}%
\vspace{7pt}\textbf{End Title}\\\lipsum[1]

\vspace{10pt}\end{minipage}}
}
\needspace{\foo}{\setlength{\parskip}{4pt}

\mysectionstyle{Experiences}

\begin{tabular}{@{}>{\RaggedRight}p{11cm}>{\RaggedLeft}p{6.35cm}@{}}\fontsize{13}{6}\selectfont{LaTeX}&\textbf{2011 to 2012}\end{tabular}\\\begin{tabular}{@{}>{\RaggedRight}p{11cm}>{\RaggedLeft}p{6.35cm}@{}}\textbf{Lead Programmer}&\textbf{}\end{tabular}%
\vspace{3pt}\begin{itemize}[parsep=0pt, topsep=0pt, itemsep=0pt, leftmargin=2ex]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\item \lipsum[1]\end{itemize}%
\vspace{7pt}\textbf{End Title}\\\lipsum[1]

\vspace{10pt}}
\end{document}


The problem is that settototalheight doesn't accept empty lines which is something i cannot control in my dynamic content. (This is a live text editor).

Is there a command or a package that accept empty lines to calculate the height of a text? The real purpose of this is that each live text editor must not break into several pages or if it does, it starts at a new page then spread to the next ones (that's the reason of using needspace).

-

Unfortunately, calc doesn't define \settototalheight (more precisely the helper macro \@settodim) as \long, so it doesn't accept empty lines in their argument.

We need to patch \@settodim to make it work. Put the following code in the preamble, after having loaded all packages:

%%% Patch calc
\makeatletter
\begingroup
\toks0=\expandafter{\@settodim{#1}{#2}{#3}}
\edef\x{\endgroup
\long\def\noexpand\@settodim##1##2##3{\the\toks0 }}\x
\makeatother


(See http://tex.stackexchange.com/a/39980/4427 for a similar answer where the same trick is used)

-
Thanks egreg. I'll check this out. It works on the example that i put, i'll try it in a real case scenario. –  Pete Jan 15 '13 at 16:36

The adjustbox environment from the package with the same name allows you to store the total height (and all other dimensions) of its content. Simply use the minipage key to allow for paragraphs in the content and at the end the gooble key to not insert the content at the current location.

\documentclass{article}

\newlength\mylength

\begin{document}

Some text

with empty

lines

It's also possible to store the content into a savebox using the gstore key, however, you might want to have a vbox not a hbox here.
Which one is faster? The \settototalheight or the adjustbox? I suppose that the ajustbox doesn't need the calc patch. Which one is more flexible? –  Pete Jan 15 '13 at 19:43
@Pete: Both store the content into a box and then store the total height of it into a length register. I would say adjustbox is more flexible if you want to adjust the contents even more before measuring the content (see the manual for all the features). The other method should be a little faster because it doesn't has to process the key=value syntax, but I don't think this matters much. –  Martin Scharrer Jan 15 '13 at 20:12