9

I'm trying to measure the height of a part of my page.

The complete titlepage, along with the part of the page I'm trying to measure is outlined in red, and the code to compile the page is shown below that. Yes, I have to measure it all at once. :-(

http://i.imgur.com/cbmNh0h.png

\documentclass[10pt]{extarticle}
\usepackage{lipsum}
\usepackage{multicol}
\usepackage[a4paper,margin=.9in]{geometry}
\usepackage{xcolor}
\definecolor{palette2}{RGB}{235,249,249}
\usepackage[letterspace=400]{microtype}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{array}
\usepackage[bitstream-charter]{mathdesign}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\usepackage{etoolbox}

\newcommand{\headrulecolor}[1]{\patchcmd{\headrule}{\hrule}{\color{#1}\hrule}{}{}}
\makeatletter
\fancypagestyle{plain}{
  \fancyhf{}
  \headrulecolor{lightgray}
  \chead{\textit{Some science institution}}
  \renewcommand{\headrulewidth}{.5pt}
}
\setlength\parindent\z@
\newcommand*\docparspacing{
\setlength\parindent{16pt}
\setlength\parskip{0pt}
}
\makeatother

\begin{document}
\thispagestyle{plain}

\raisebox{3.5ex}{\hbadness=9999
\colorbox{palette2}{\vrule width 0pt height 5.8ex \begin{tabular}{>{\RaggedRight\arraybackslash}p{\dimexpr.3\linewidth-\tabcolsep} >{\centering\arraybackslash}p{\dimexpr.35\linewidth-2\tabcolsep} >{\RaggedLeft\arraybackslash}p{\dimexpr.35\linewidth-4.4\tabcolsep}}
&
&
\end{tabular}}}
\vskip 2ex
\textcolor{lightgray}{\hrule height .5pt}
\vskip 6ex
\Huge{A title in fontsize Huge}
\vskip .4ex
\large{Doe, J. (John)}
\vskip 2ex
\normalsize
\vskip .5ex
\valign{%
    \hsize=\dimexpr.5\linewidth-2\tabcolsep
    \vskip 2ex
    #\vfil\nointerlineskip
    \vskip 1.5ex
    \cr
    \noindent\textls{\itshape\footnotesize KEYWORDS}
    \vskip -1.5ex
    \hbox to\hsize{\color{lightgray}\leaders\hrule height .5pt\hfill}
    \vskip 1.5ex
    \itshape{obituary

    linguist

    typography}\cr
    \noalign{\hfill}
    \noindent\textls{\itshape\footnotesize ABSTRACT}
    \vskip -1.5ex
    \hbox to\hsize{\color{lightgray}\leaders\hrule height .5pt\hfill}
    \vskip 1.5ex

    \small \lipsum[4] \cr
  }\par
  \vskip2ex

  \hbox to\linewidth{\color{lightgray}\leaders\hrule height .5pt\hfill}
\vskip 3ex

\colorbox{palette2}{%
\begin{minipage}{\dimexpr\textwidth-2\fboxsep}
\lipsum[1-2]
\end{minipage}%
}
\vskip 5ex
\docparspacing
\begin{multicols*}{3}
\section{Document structure}
\lipsum[1-3]
\end{multicols*}
\end{document}

I tried measuring this by creating my own box, which compiles with eight errors and requires extensive use of \hbox and \vbox. It also required me to replace every instance of \hrule with a \leaders variant, which requires glue (the bane of my existence). I strongly suspect this to be the culprit of the incorrect output. However, my main question is, my current approach seems particularly cumbersome, which leads me to believe my approach is not the right one, i.e. an approach more flexible (or "easy" if you wish) exists. How can I create this box for measurement and usage in the document, in the most accessible/flexible manner?

http://i.imgur.com/o89CdlS.png?1

\documentclass[10pt]{extarticle}
\usepackage{lipsum}
\usepackage{multicol}
\usepackage[a4paper,margin=.9in]{geometry}
\usepackage{xcolor}
\definecolor{palette2}{RGB}{235,249,249}
\usepackage[letterspace=400]{microtype}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{array}
\usepackage[bitstream-charter]{mathdesign}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\usepackage{etoolbox}

\newcommand{\headrulecolor}[1]{\patchcmd{\headrule}{\hrule}{\color{#1}\hrule}{}{}}
\makeatletter
\fancypagestyle{plain}{
  \fancyhf{}
  \headrulecolor{lightgray}
  \chead{\textit{Some science institution}}
  \renewcommand{\headrulewidth}{.5pt}
}
\setlength\parindent\z@
\newcommand*\docparspacing{
\setlength\parindent{16pt}
\setlength\parskip{0pt}
}
\makeatother

\newsavebox\titlebox
\setbox\titlebox=\vbox{%
\hbox{\raisebox{3.5ex}{\hbadness=9999
\colorbox{palette2}{\vrule width 0pt height 5.8ex \begin{tabular}{>{\RaggedRight\arraybackslash}p{\dimexpr.3\linewidth-\tabcolsep} >{\centering\arraybackslash}p{\dimexpr.35\linewidth-2\tabcolsep} >{\RaggedLeft\arraybackslash}p{\dimexpr.35\linewidth-4.4\tabcolsep}}
&
&
\end{tabular}}}
}
\vskip 2ex
\hbox to \linewidth{\textcolor{lightgray}{\leaders\hrule height .5pt\hfil}}
\vskip 6ex
\hbox{\Huge{A title in fontsize Huge}}
\vskip .4ex
\hbox{\large{Doe, J. (John)}}
\vskip 2ex
\normalsize
\vskip .5ex
\valign{%
    \hsize=\dimexpr.5\linewidth-2\tabcolsep
    \vskip 2ex
    #\vfil\nointerlineskip
    \vskip 1.5ex
    \cr
    \noindent\textls{\itshape\footnotesize KEYWORDS}
    \vskip -1.5ex
    \hbox to\hsize{\color{lightgray}\leaders\hrule height .5pt\hfil}
    \vskip 1.5ex
    \itshape{obituary

    linguist

    typography}\cr
    \noalign{\hfill}
    \noindent\textls{\itshape\footnotesize ABSTRACT}
    \vskip -1.5ex
    \hbox to\hsize{\color{lightgray}\leaders\hrule height .5pt\hfil}
    \vskip 1.5ex

    \small \lipsum[4] \cr
  }\par
\vskip2ex
\hbox to\linewidth{\color{lightgray}\leaders\hrule height .5pt\hfil}
\vskip 3ex
\colorbox{palette2}{%
\begin{minipage}{\dimexpr\textwidth-2\fboxsep}
  \lipsum[1-2]
\end{minipage}%
}
\vskip 5ex
}

\begin{document}
\thispagestyle{plain}

\usebox\titlebox
\docparspacing
\begin{multicols*}{3}
\section{Document structure}
\lipsum[1-3]
\end{multicols*}
\end{document}
  • \the\ht\titlebox tells how tall is your box. \tracingpages1 may also help. – Symbol 1 Sep 26 '14 at 0:54
6

You may use the tcolorbox package (version 3.12 (2014/07/29) or newer) to do the measuring. In my solution, the kernel is an environment measurebox constructed by the following code:

\makeatletter
\newtcolorbox{measurebox}[1][]{blank,parbox=false,
  show bounding box,
  finish={\node[draw,fill=white,fill opacity=0.85,inner sep=5mm,
    rounded corners,align=left,node font=\Large\bfseries]
    at (frame.center) { width: \tcb@width,\\ height: \tcb@height};},
  #1}
\makeatother

Any paragraph encircled by this environment tcolorbox is measured and its dimension is written on the screen or paper. The complete code is:

\documentclass[10pt]{extarticle}
\usepackage{lipsum}
\usepackage{multicol}
\usepackage[a4paper,margin=.9in]{geometry}
\usepackage{xcolor}
\definecolor{palette2}{RGB}{235,249,249}
\usepackage[letterspace=400]{microtype}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{array}
\usepackage[bitstream-charter]{mathdesign}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\usepackage{etoolbox}
\usepackage[many]{tcolorbox}% version `3.12 (2014/07/29)` or newer

\makeatletter% construct boxing environment
\newtcolorbox{measurebox}[1][]{blank,parbox=false,
  show bounding box,
  finish={\node[draw,fill=white,fill opacity=0.85,inner sep=5mm,
    rounded corners,align=left,node font=\Large\bfseries]
    at (frame.center) { width: \tcb@width,\\ height: \tcb@height};},
  #1}
\makeatother% end construction

\newcommand{\headrulecolor}[1]{\patchcmd{\headrule}{\hrule}{\color{#1}\hrule}{}{}}
\makeatletter
\fancypagestyle{plain}{
  \fancyhf{}
  \headrulecolor{lightgray}
  \chead{\textit{Some science institution}}
  \renewcommand{\headrulewidth}{.5pt}
}
\setlength\parindent\z@
\newcommand*\docparspacing{
\setlength\parindent{16pt}
\setlength\parskip{0pt}
}
\makeatother

\begin{document}
\thispagestyle{plain}

\begin{measurebox}%----- start measuring -----
\raisebox{3.5ex}{\hbadness=9999
\colorbox{palette2}{\vrule width 0pt height 5.8ex \begin{tabular}{>{\RaggedRight\arraybackslash}p{\dimexpr.3\linewidth-\tabcolsep} >{\centering\arraybackslash}p{\dimexpr.35\linewidth-2\tabcolsep} >{\RaggedLeft\arraybackslash}p{\dimexpr.35\linewidth-4.4\tabcolsep}}
&
&
\end{tabular}}}
\vskip 2ex
\textcolor{lightgray}{\hrule height .5pt}
\vskip 6ex
\Huge{A title in fontsize Huge}
\vskip .4ex
\large{Doe, J. (John)}
\vskip 2ex
\normalsize
\vskip .5ex
\valign{%
    \hsize=\dimexpr.5\linewidth-2\tabcolsep
    \vskip 2ex
    #\vfil\nointerlineskip
    \vskip 1.5ex
    \cr
    \noindent\textls{\itshape\footnotesize KEYWORDS}
    \vskip -1.5ex
    \hbox to\hsize{\color{lightgray}\leaders\hrule height .5pt\hfill}
    \vskip 1.5ex
    \itshape{obituary

    linguist

    typography}\cr
    \noalign{\hfill}
    \noindent\textls{\itshape\footnotesize ABSTRACT}
    \vskip -1.5ex
    \hbox to\hsize{\color{lightgray}\leaders\hrule height .5pt\hfill}
    \vskip 1.5ex

    \small \lipsum[4] \cr
  }\par
  \vskip2ex

  \hbox to\linewidth{\color{lightgray}\leaders\hrule height .5pt\hfill}
\vskip 3ex

\colorbox{palette2}{%
\begin{minipage}{\dimexpr\textwidth-2\fboxsep}
\lipsum[1-2]
\end{minipage}%
}
\vskip 5ex
\end{measurebox}%----- end measuring -----
\docparspacing
\begin{multicols*}{3}
\section{Document structure}
\lipsum[1-3]
\end{multicols*}
\end{document}

enter image description here

The environment measurebox takes an optional argument which can be used to influence the box. In this application, it may be useful to change the spacing before or after the box, e.g. by

\begin{measurebox}[before=\par\bigskip,
                   after=\par\vskip 10ex]
...

Second Solution:

This is a variant of the first part. Now, the height of the box is saved into a mandatory parameter of measurebox for further usage:

\makeatletter
\newtcolorbox{measurebox}[2][]{blank,parbox=false,
  overlay={\xdef#2{\tcb@height}},
  #1}
\makeatother

It is used like the following:

\begin{measurebox}{\myheight}

All together, this is:

\documentclass[10pt]{extarticle}
\usepackage{lipsum}
\usepackage{multicol}
\usepackage[a4paper,margin=.9in]{geometry}
\usepackage{xcolor}
\definecolor{palette2}{RGB}{235,249,249}
\usepackage[letterspace=400]{microtype}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{array}
\usepackage[bitstream-charter]{mathdesign}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\usepackage{etoolbox}
\usepackage[many]{tcolorbox}

\makeatletter
\newtcolorbox{measurebox}[2][]{blank,parbox=false,
  overlay={\xdef#2{\tcb@height}},
  #1}
\makeatother

\newcommand{\headrulecolor}[1]{\patchcmd{\headrule}{\hrule}{\color{#1}\hrule}{}{}}
\makeatletter
\fancypagestyle{plain}{
  \fancyhf{}
  \headrulecolor{lightgray}
  \chead{\textit{Some science institution}}
  \renewcommand{\headrulewidth}{.5pt}
}
\setlength\parindent\z@
\newcommand*\docparspacing{
\setlength\parindent{16pt}
\setlength\parskip{0pt}
}
\makeatother

\begin{document}
\thispagestyle{plain}

\begin{measurebox}{\myheight}
\raisebox{3.5ex}{\hbadness=9999
\colorbox{palette2}{\vrule width 0pt height 5.8ex \begin{tabular}{>{\RaggedRight\arraybackslash}p{\dimexpr.3\linewidth-\tabcolsep} >{\centering\arraybackslash}p{\dimexpr.35\linewidth-2\tabcolsep} >{\RaggedLeft\arraybackslash}p{\dimexpr.35\linewidth-4.4\tabcolsep}}
&
&
\end{tabular}}}
\vskip 2ex
\textcolor{lightgray}{\hrule height .5pt}
\vskip 6ex
\Huge{A title in fontsize Huge}
\vskip .4ex
\large{Doe, J. (John)}
\vskip 2ex
\normalsize
\vskip .5ex
\valign{%
    \hsize=\dimexpr.5\linewidth-2\tabcolsep
    \vskip 2ex
    #\vfil\nointerlineskip
    \vskip 1.5ex
    \cr
    \noindent\textls{\itshape\footnotesize KEYWORDS}
    \vskip -1.5ex
    \hbox to\hsize{\color{lightgray}\leaders\hrule height .5pt\hfill}
    \vskip 1.5ex
    \itshape{obituary

    linguist

    typography}\cr
    \noalign{\hfill}
    \noindent\textls{\itshape\footnotesize ABSTRACT}
    \vskip -1.5ex
    \hbox to\hsize{\color{lightgray}\leaders\hrule height .5pt\hfill}
    \vskip 1.5ex

    \small \lipsum[4] \cr
  }\par
  \vskip2ex

  \hbox to\linewidth{\color{lightgray}\leaders\hrule height .5pt\hfill}
\vskip 3ex

\colorbox{palette2}{%
\begin{minipage}{\dimexpr\textwidth-2\fboxsep}
\lipsum[1-2]
\end{minipage}%
}
\vskip 5ex
\end{measurebox}
\docparspacing
\begin{multicols*}{3}
\section{Document structure}
\textcolor{red}{The box above has a height of \myheight.}\par
\lipsum[1-3]
\end{multicols*}
\end{document}

enter image description here

Third Solution:

The third solution omits tcolorbox completely. A macro \measurebox is created by

\newcommand{\measurebox}[4]{%
  \sbox{#1}{\begin{minipage}{#2}#4%
  \end{minipage}}%
  \edef#3{\the\dimexpr\ht#1+\dp#1\relax}%
}

It takes four parameters: #1 is a valid boxname, #2 is a width, #3 is a macro name with will get the total height of the box, #4 is the box content. It is used like the following:

\newsavebox\titlebox
\measurebox{\titlebox}{\textwidth}{\myheight}{%----- start measuring
...
}%----- end measuring
%
\usebox\titlebox%  somewhere
The box above has a height of \myheight.% somewhere

The complete code with the same output as above is:

\documentclass[10pt]{extarticle}
\usepackage{lipsum}
\usepackage{multicol}
\usepackage[a4paper,margin=.9in]{geometry}
\usepackage{xcolor}
\definecolor{palette2}{RGB}{235,249,249}
\usepackage[letterspace=400]{microtype}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{array}
\usepackage[bitstream-charter]{mathdesign}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\usepackage{etoolbox}

\makeatletter
\newcommand{\measurebox}[4]{%
  \sbox{#1}{\begin{minipage}{#2}#4%
  \end{minipage}}%
  \edef#3{\the\dimexpr\ht#1+\dp#1\relax}%
}
\makeatother

\newcommand{\headrulecolor}[1]{\patchcmd{\headrule}{\hrule}{\color{#1}\hrule}{}{}}
\makeatletter
\fancypagestyle{plain}{
  \fancyhf{}
  \headrulecolor{lightgray}
  \chead{\textit{Some science institution}}
  \renewcommand{\headrulewidth}{.5pt}
}
\setlength\parindent\z@
\newcommand*\docparspacing{
\setlength\parindent{16pt}
\setlength\parskip{0pt}
}
\makeatother

\begin{document}
\thispagestyle{plain}

\newsavebox\titlebox
\measurebox{\titlebox}{\textwidth}{\myheight}{%----- start measuring
\raisebox{3.5ex}{\hbadness=9999
\colorbox{palette2}{\vrule width 0pt height 5.8ex \begin{tabular}{>{\RaggedRight\arraybackslash}p{\dimexpr.3\linewidth-\tabcolsep} >{\centering\arraybackslash}p{\dimexpr.35\linewidth-2\tabcolsep} >{\RaggedLeft\arraybackslash}p{\dimexpr.35\linewidth-4.4\tabcolsep}}
&
&
\end{tabular}}}
\vskip 2ex
\textcolor{lightgray}{\hrule height .5pt}
\vskip 6ex
\Huge{A title in fontsize Huge}
\vskip .4ex
\large{Doe, J. (John)}
\vskip 2ex
\normalsize
\vskip .5ex
\valign{%
    \hsize=\dimexpr.5\linewidth-2\tabcolsep
    \vskip 2ex
    #\vfil\nointerlineskip
    \vskip 1.5ex
    \cr
    \noindent\textls{\itshape\footnotesize KEYWORDS}
    \vskip -1.5ex
    \hbox to\hsize{\color{lightgray}\leaders\hrule height .5pt\hfill}
    \vskip 1.5ex
    \itshape{obituary

    linguist

    typography}\cr
    \noalign{\hfill}
    \noindent\textls{\itshape\footnotesize ABSTRACT}
    \vskip -1.5ex
    \hbox to\hsize{\color{lightgray}\leaders\hrule height .5pt\hfill}
    \vskip 1.5ex

    \small \lipsum[4] \cr
  }\par
  \vskip2ex

  \hbox to\linewidth{\color{lightgray}\leaders\hrule height .5pt\hfill}
\vskip 3ex

\colorbox{palette2}{%
\begin{minipage}{\dimexpr\textwidth-2\fboxsep}
\lipsum[1-2]
\end{minipage}%
}
\vskip 5ex
}%----- end measuring
%
\usebox\titlebox

\docparspacing
\begin{multicols*}{3}
\section{Document structure}
\textcolor{red}{The box above has a height of \myheight.}\par
\lipsum[1-3]
\end{multicols*}
\end{document}

Note that you have to use the mesuring after \begin{document}, but you can measure as many boxes as you like before using any of them.

  • Thanks. This will do as a preliminary answer. Did you choose tcolorbox because of affinity with the package? – 1010011010 Sep 26 '14 at 8:47
  • @1010011010 Yes, I'm the author of this package and so I have a large affinity with it ;-) Also, I used it for similar measuring problems since the package calculates the dimensions anyway. – Thomas F. Sturm Sep 26 '14 at 9:00
  • @1010011010 Do you just want to know the height of your box or do you need the value for further computations? My answer, so far, just displays the dimensions. – Thomas F. Sturm Sep 26 '14 at 11:18
  • I need the height for further computations, but I can think of a workaround. If you want to include it in your answer, I won't stop you from doing so. – 1010011010 Sep 26 '14 at 14:24
  • @1010011010 I've updated my answer with an according variant. – Thomas F. Sturm Sep 26 '14 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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