# Automatically adjusting size of a box based on other content

The following code creates chapter titles with a red bar of adjustable height. Currently, I have to potentially adjust the height for each chapter based on the included mini table of contents. Is there any way to automatically calculate the height of that mini table of contents and use that value to set the height of the red bar.

\documentclass[11pt,openany]{memoir}
\usepackage{titletoc}
\usepackage{lipsum}

\usepackage{color,calc}
\definecolor{ChapRed}{rgb}{.600,.100,.100}

%% adapted from BlueBox style, pp:43-44
% http://mirror.csclub.uwaterloo.ca/CTAN/info/latex-samples/MemoirChapStyles/MemoirChapStyles.pdf
\newcommand{\RedBarLength}{5em}

\newsavebox{\ChpNumBox}
\makeatletter
\newcommand*{\thickhrulefill}{%
\leavevmode\leaders\hrule height 1\p@ \hfill \kern \z@}
\newcommand*\BuildChpNum[2]{%
\begin{tabular}[t]{@{}c@{}}
\makebox[0pt][c]{#1\strut} \\[.5ex]
\colorbox{ChapRed}{%
\rule[-\RedBarLength]{0pt}{0pt}%
\rule{1ex}{0pt}\color{white}#2\strut
\rule{1ex}{0pt}}%
\end{tabular}}
\makechapterstyle{RedBox}{%
\renewcommand{\chapnamefont}{\large\scshape}
\setlength{\beforechapskip}{-30pt}
\setlength{\midchapskip}{10pt}
\setlength{\afterchapskip}{30pt}
\renewcommand{\printchaptername}{}
\renewcommand{\printchapternum}{%
\sbox{\ChpNumBox}{%
\BuildChpNum{\chapnamefont\@chapapp}%
{\chapnumfont\thechapter}}}
\renewcommand{\printchapternonum}{%
\sbox{\ChpNumBox}{%
\BuildChpNum{\chapnamefont\vphantom{\@chapapp}}%
{\chapnumfont\hphantom{\thechapter}}}}
\renewcommand{\afterchapternum}{}
\renewcommand{\printchaptertitle}[1]{%
\usebox{\ChpNumBox}\hfill
\parbox[t]{\hsize-\wd\ChpNumBox-1em}{%
\vspace{\midchapskip}%
\thickhrulefill\\[10pt]
{\chaptitlefont \textcolor{ChapRed}{##1}}\\
\startcontents[chapters]
\printcontents[chapters]{}{1}{\color{ChapRed}}
}}%
}
\chapterstyle{RedBox}

\begin{document}
\tableofcontents
\chapter{A chapter}
\section{Section}
\lipsum[1]
\section[Short title for this section]{Section 2 with a very, very long title which probably takes more than one line}
\lipsum[1]

\renewcommand{\RedBarLength}{19em}   %% How to automate this ?

\chapter{Second chapter}
\section{Section}
\lipsum[1]
\section{Another section}
\lipsum[1]
\section{Another section}
\section{Another section}
\section{Another section}
\section{Another section}
\section{Another section}
\section{Another section}
\section{Another section}
\section{Another section}
\section{Another section}
\section{Another section}
\section{Another section}
\end{document}

-

I tried the following:

• put the mini-contents in a box to be measured
• use the measured length to construct the bar
• \RedBarLengthserves only as an addition measuring the chapter title itself.

Here's the MWE again. I had to shuffle things around a bit to have the measurement available at the right time.

\documentclass[11pt,openany]{memoir}
\usepackage{titletoc}
\usepackage{lipsum}

\usepackage{color,calc}
\definecolor{ChapRed}{rgb}{.600,.100,.100}

%% adapted from BlueBox style, pp:43-44
% http://mirror.csclub.uwaterloo.ca/CTAN/info/latex-samples/MemoirChapStyles/MemoirChapStyles.pdf
\newcommand{\RedBarLength}{3em}

\newsavebox{\ChpNumBox}
\newsavebox{\ChpContBox}
\makeatletter
\newcommand*{\thickhrulefill}{%
\leavevmode\leaders\hrule height 1\p@ \hfill \kern \z@}
\newcommand*\BuildChpNum[3]{%
\begin{tabular}[t]{@{}c@{}}
\makebox[0pt][c]{#1\strut} \\[.5ex]
\colorbox{ChapRed}{%
\rule[-\RedBarLength-(#3)]{0pt}{0pt}%
\rule{1ex}{0pt}\color{white}#2\strut
\rule{1ex}{0pt}}%
\end{tabular}}
\makechapterstyle{RedBox}{%
\renewcommand{\chapnamefont}{\large\scshape}
\setlength{\beforechapskip}{-30pt}
\setlength{\midchapskip}{10pt}
\setlength{\afterchapskip}{30pt}
\renewcommand{\printchaptername}{}
\renewcommand{\printchapternum}{%
\startcontents[chapters]
\sbox{\ChpContBox}{%
\parbox{\linewidth}{%
\printcontents[chapters]{}{1}{\color{ChapRed}}%
}}%
\sbox{\ChpNumBox}{%
\BuildChpNum{\chapnamefont\@chapapp}%
{\chapnumfont\thechapter}%
{\ht\ChpContBox+\dp\ChpContBox}%
}}
\renewcommand{\printchapternonum}{%
\startcontents[chapters]
\sbox{\ChpContBox}{%
\parbox{\linewidth}{%
\printcontents[chapters]{}{1}{\color{ChapRed}}%
}}%
\sbox{\ChpNumBox}{%
\BuildChpNum{\chapnamefont\vphantom{\@chapapp}}%
{\chapnumfont\hphantom{\thechapter}}%
{\ht\ChpContBox+\dp\ChpContBox}%
}}
\renewcommand{\afterchapternum}{}
\renewcommand{\printchaptertitle}[1]{%
\usebox{\ChpNumBox}\hfill
\parbox[t]{\hsize-\wd\ChpNumBox-1em}{%
\vspace{\midchapskip}%
\thickhrulefill\\[10pt]
{\chaptitlefont \textcolor{ChapRed}{##1}}\\
\printcontents[chapters]{}{1}{\color{ChapRed}}%
}}%
}
\chapterstyle{RedBox}

\begin{document}
\tableofcontents
\chapter{A chapter}
\section{Section}
\lipsum[1]
\section[Short title for this section]{Section 2 with a very, very long title which probably takes more than one line}
\lipsum[1]

% \renewcommand{\RedBarLength}{19em}   %% How to automate this ?

\chapter{Second chapter}
\section{Section}
\lipsum[1]
\section{Another section}
\lipsum[1]
\section{Another section}
\section{Another section}
\section{Another section}
\section{Another section}
\section{Another section}
\section{Another section}
\section{Another section}
\section{Another section}
\section{Another section}
\section{Another section}
\section{Another section}
\end{document}

-
I'm trying to understand how this works... Shouldn't there be a \makeatother to "undo" the effect of \makeatletter? (I'm new to this...) –  André Apr 2 '12 at 19:11
@André Yes, why not? I just copied and modified your MWE which also doesn't contain \makeatother. You can insert \makeatother at any point after the last use of @ in a control sequence. –  Stephan Lehmke Apr 2 '12 at 21:15