Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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{}

\fancyheadoffset{0in}
\headheight = 53pt

\renewcommand{\headrulewidth}{0pt}

\setmainfont{Calibri}

% ------------------------------------------------------------------------------------
% Defining the section style
\newcommand{\mysectionstyle}[1]{\colorbox{shadecolor}{\begin{tabular}{>{}p{\rectanglelength}}{\fontsize{13}{6}\selectfont\textbf{#1}}\end{tabular}}\vspace{6pt}}
% ------------------------------------------------------------------------------------

\addtolength{\footskip}{0.6cm}
\renewcommand{\footrulewidth}{1pt}
\renewcommand{\footrule}{{\color{light-blue}%
    \vskip-\footruleskip\vskip-\footrulewidth
    \hrule width\headwidth height\footrulewidth\vskip\footruleskip}}

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

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

\newlength{\rectanglelength}
\setlength{\rectanglelength}{\textwidth}
\addtolength{\rectanglelength}{-6pt}

\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).

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

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)

share|improve this answer
    
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
add comment

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}

\usepackage{adjustbox}

\newlength\mylength

\begin{document}

\begin{adjustbox}{minipage=\linewidth,gstore totalheight=\mylength,gobble}
Some text

with empty

lines
\end{adjustbox}

Total height is \the\mylength.

\end{document}

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.

share|improve this answer
    
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
    
Thank you for your informations –  Pete Jan 15 '13 at 22:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.