根据其他内容自动调整框的大小

根据其他内容自动调整框的大小

以下代码创建了带有可调节高度的红色条的章节标题。目前,我必须根据所包含的迷你目录调整每个章节的高度。有没有办法自动计算该迷你目录的高度并使用该值设置红色条的高度。

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

答案1

我尝试了以下操作:

  • 将小件物品放入要测量的盒子中
  • 使用测量的长度来构建条形图
  • \RedBarLength仅作为衡量章节标题本身的补充。

这是 MWE。我不得不稍微调整一下,以便在正确的时间获得测量结果。

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

相关内容