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.

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}

\setsecheadstyle{\color{ChapRed}\large\bfseries}

%% 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{\chapternamenum}{}
\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}
share|improve this question

1 Answer 1

up vote 5 down vote accepted

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}

\setsecheadstyle{\color{ChapRed}\large\bfseries}

%% 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{\chapternamenum}{}
  \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}
share|improve this answer
    
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

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.