根据当前章节返回长度的宏

根据当前章节返回长度的宏

我正在使用 scrbook 编写文档,并且定义了自定义章节样式,其中我使用 tikz 绘制了一个大章节编号:

\renewcommand*{\chapterformat}{%
\begin{tikzpicture}[remember picture, overlay]
    \node[anchor=south east, yshift=1.2cm, xshift=\textwidth,
          inner sep=0, outer sep=0]{%
        \fontsize{10cm}{10cm}\selectfont%
        \textcolor{laccentcolor}{\thechapter}%
    };
    % alignment line
    \draw[thin] (current page text area.north east)
        -- (current page text area.south east);
\end{tikzpicture}%
}

章节编号应该与文本区域的右侧对齐,但是由于数字周围的空格是字符的一部分,因此并没有对齐:

在此处输入图片描述

每个数字到右边框的距离都不同。为了实现完美对齐,我想根据每个章节调整章节编号的位置。我的想法是定义一个命令,根据当前的值返回不同的长度\thechapter,然后使用此命令定义xshift章节编号的:

\newcommand*{\chapteroffset}{%
\IfEndWith{\thechapter}{1}{\textwidth+15mm}{%
\IfEndWith{\thechapter}{2}{\textwidth+5mm}{%
\IfEndWith{\thechapter}{3}{\textwidth+6mm}{%
\textwidth+0mm%
}}}%
}

\renewcommand*{\chapterformat}{%
\begin{tikzpicture}[remember picture, overlay]
    \node[anchor=south east, yshift=1.2cm, xshift=\chapteroffset,
          inner sep=0, outer sep=0]{%
        \fontsize{10cm}{10cm}\selectfont%
        \textcolor{laccentcolor}{\thechapter}%
    };
    % alignment line
    \draw[thin] (current page text area.north east)
        -- (current page text area.south east);
\end{tikzpicture}%
}

我尝试了多种不同的方法来实现这一点,但到目前为止,所有这些方法都导致了某种无限循环,导致我的文档无法完成构建。我怀疑原因是我没有使用正确的方法从 if 表达式返回维度。

如何从随当前章节变化的宏中返回尺寸?请注意,它还需要适用于附录中的非整数。或者,我如何以不同的方式实现图片中的外观?

这是我的当前 MWE 状态:

\documentclass[BCOR=15mm, DIV=8]{scrbook}

\KOMAoptions{
    headings=twolinechapter,
    chapterprefix=false,
    numbers=noenddot
}

\usepackage{typearea}
\usepackage[utf8]{inputenc}
\usepackage{kpfonts}
\usepackage[T1]{fontenc}
\usepackage{microtype}
\usepackage{lipsum}

\usepackage{tikz}
\usetikzlibrary{
    calc,
    positioning}
\tikzset{>=latex}
\usepackage{tikzpagenodes}

\usepackage{etoolbox}
\usepackage{xstring}
\usepackage{calc}

\definecolor{laccentcolor}{HTML}{d3d3d3}


\addtokomafont{disposition}{\rmfamily}

% Macro that determines the per-chapter offsets.
% If I use this definition, the document will not finish building.
% \newcommand*{\chapteroffset}{%
% \IfEndWith{\thechapter}{1}{\textwidth+15mm}{%
% \IfEndWith{\thechapter}{2}{\textwidth+5mm}{%
% \IfEndWith{\thechapter}{3}{\textwidth+6mm}{%
% \textwidth+0mm%
% }}}%
% }

% returning a fixed length from the macro like this works
\newcommand*{\chapteroffset}{\textwidth+5mm}

\addtokomafont{chapter}{\scshape\LARGE}

\renewcommand*{\chapterformat}{%
\begin{tikzpicture}[remember picture, overlay]
    \node[anchor=south east, yshift=1.2cm, xshift=\chapteroffset,
          inner sep=0, outer sep=0]{%
        \fontsize{10cm}{10cm}\selectfont%
        \textcolor{laccentcolor}{\thechapter}%
    };
    % alignment line
    \draw[thin] (current page text area.north east)
        -- (current page text area.south east);
\end{tikzpicture}%
}
\renewcommand*{\chapterheadstartvskip}{\vspace*{10cm}}

\begin{document}
    \chapter{A Fancy Chapter Name to Test the Formatting}
    \lipsum[1]

    \chapter{A Fancy Chapter Name to Test the Formatting}
    \lipsum[1]

    \chapter{A Fancy Chapter Name to Test the Formatting}
    \lipsum[1]
\end{document}

答案1

您可以将班次存储在可以使用的数组中。由于您正在加载tikzpagenodes,因此我还建议使用该包。

\documentclass[BCOR=15mm, DIV=8]{scrbook}

\KOMAoptions{
    headings=twolinechapter,
    chapterprefix=false,
    numbers=noenddot
}

\usepackage{typearea}
\usepackage[utf8]{inputenc}
\usepackage{kpfonts}
\usepackage[T1]{fontenc}
\usepackage{microtype}
\usepackage{lipsum}

\usepackage{tikz}
\usetikzlibrary{
    calc,
    positioning}
\tikzset{>=latex}
\usepackage{tikzpagenodes}

\usepackage{etoolbox}
\usepackage{xstring}
\usepackage{calc}

\definecolor{laccentcolor}{HTML}{d3d3d3}


\addtokomafont{disposition}{\rmfamily}


% returning a fixed length from the macro like this works
\newcommand*{\chapteroffset}{\textwidth+5mm}

\addtokomafont{chapter}{\scshape\LARGE}

\def\chaplengths{{11mm,5mm,6mm}}
\renewcommand*{\chapterformat}{%
\begin{tikzpicture}[remember picture]
\coordinate (mychapanchor-\arabic{chapter});
\end{tikzpicture}
\begin{tikzpicture}[remember picture, overlay]
    \pgfmathsetmacro{\mylength}{\chaplengths[\arabic{chapter}-1]}
    \node[anchor=south east,xshift=\mylength,
          inner sep=0, outer sep=0]
          at ([yshift=1.2cm]mychapanchor-\arabic{chapter}-| current page text area.east){%
        \fontsize{10cm}{10cm}\selectfont%
        \textcolor{laccentcolor}{\thechapter}%
    };
    % alignment line
    \draw[thin] (current page text area.north east)
        -- (current page text area.south east);
\end{tikzpicture}%
}
\renewcommand*{\chapterheadstartvskip}{\vspace*{10cm}}

\begin{document}
    \chapter{A Fancy Chapter Name to Test the Formatting}
    \lipsum[1]

    \chapter{A Fancy Chapter Name to Test the Formatting}
    \lipsum[1]

    \chapter{A Fancy Chapter Name to Test the Formatting}
    \lipsum[1]
\end{document}

在此处输入图片描述

答案2

您可以\int_case:nnF使用expl3

\documentclass[BCOR=15mm, DIV=8]{scrbook}

\KOMAoptions{
    headings=twolinechapter,
    chapterprefix=false,
    numbers=noenddot
}

\usepackage{typearea}
\usepackage[utf8]{inputenc}
\usepackage{kpfonts}
\usepackage[T1]{fontenc}
\usepackage{microtype}
\usepackage{lipsum,showframe}

\usepackage{tikz}
\usetikzlibrary{
    calc,
    positioning}
\tikzset{>=latex}
\usepackage{tikzpagenodes}

\usepackage{xparse}

\definecolor{laccentcolor}{HTML}{d3d3d3}


\addtokomafont{disposition}{\rmfamily}

% Macro that determines the per-chapter offsets.
\ExplSyntaxOn
\NewExpandableDocumentCommand{\chapteroffset}{}
 {
  \textwidth+
  \int_case:nnF { \int_mod:nn { \value{chapter} } { 10 } }
   {
    {1}{11mm} % shift for last digit 1
    {2}{5mm} % shift for last digit 2
    {3}{6mm} % shift for last digit 3
   }
   {1mm} % shift for all other cases
 }
\ExplSyntaxOff

\addtokomafont{chapter}{\scshape\LARGE}

\renewcommand*{\chapterformat}{%
\begin{tikzpicture}[remember picture, overlay]
    \node[anchor=south east, yshift=1.2cm, xshift=\chapteroffset,
          inner sep=0, outer sep=0]{%
        \fontsize{10cm}{10cm}\selectfont%
        \textcolor{laccentcolor}{\thechapter}%
    };
    % alignment line
    \draw[thin] (current page text area.north east)
        -- (current page text area.south east);
\end{tikzpicture}%
}
\renewcommand*{\chapterheadstartvskip}{\vspace*{10cm}}

\begin{document}
    \chapter{A Fancy Chapter Name to Test the Formatting}
    \lipsum[1]

    \chapter{A Fancy Chapter Name to Test the Formatting}
    \lipsum[1]

    \chapter{A Fancy Chapter Name to Test the Formatting}
    \lipsum[1]
\end{document}

在此处输入图片描述

如果要处理附录,请添加“变量命令”。也许scrbook有一个内置条件来测试它是否在附录部分。如果是这样,使用它代替自制的应该不难\ifappendix

\documentclass[BCOR=15mm, DIV=8]{scrbook}

\KOMAoptions{
    headings=twolinechapter,
    chapterprefix=false,
    numbers=noenddot
}

\usepackage{typearea}
\usepackage[utf8]{inputenc}
\usepackage{kpfonts}
\usepackage[T1]{fontenc}
\usepackage{microtype}
\usepackage{lipsum,showframe}

\usepackage{tikz}
\usetikzlibrary{
    calc,
    positioning}
\tikzset{>=latex}
\usepackage{tikzpagenodes}

\usepackage{xparse}

\definecolor{laccentcolor}{HTML}{d3d3d3}


\addtokomafont{disposition}{\rmfamily}

% Macro that determines the per-chapter offsets.
\NewExpandableDocumentCommand{\chapteroffset}{}
 {
  \textwidth+
  \ifappendix\offsetforchapter\else\offsetforappendix\fi
 }
\newif\ifappendix
\ExplSyntaxOn
\NewExpandableDocumentCommand{\offsetforchapter}{}
 {
  \int_case:nnF { \int_mod:nn { \value{chapter} } { 10 } }
   {
    {1}{11mm}
    {2}{5mm}
    {3}{6mm}
   }
   {1mm} % all other cases
 }
\NewExpandableDocumentCommand{\offsetforappendix}{}
 {
  \int_case:nnF { \value{chapter} }
   {
    {1}{11mm} % offset for A
    {2}{5mm}  % offset for B
    {3}{6mm}  % offset for C
   }
   {1mm} % all other cases
 }
\ExplSyntaxOff

\addtokomafont{chapter}{\scshape\LARGE}

\renewcommand*{\chapterformat}{%
\begin{tikzpicture}[remember picture, overlay]
    \node[anchor=south east, yshift=1.2cm, xshift=\chapteroffset,
          inner sep=0, outer sep=0]{%
        \fontsize{10cm}{10cm}\selectfont%
        \textcolor{laccentcolor}{\thechapter}%
    };
    % alignment line
    \draw[thin] (current page text area.north east)
        -- (current page text area.south east);
\end{tikzpicture}%
}
\renewcommand*{\chapterheadstartvskip}{\vspace*{10cm}}

\begin{document}

\chapter{A Fancy Chapter Name to Test the Formatting}
\lipsum[1-2]

\chapter{A Fancy Chapter Name to Test the Formatting}
\lipsum[1-2]

\chapter{A Fancy Chapter Name to Test the Formatting}
\lipsum[1-2]

\appendix\appendixtrue

\chapter{A Fancy Chapter Name to Test the Formatting}
\lipsum[1-2]

\end{document}

答案3

根据 marmot 和 egreg 的回答,我最终使用了以下方法:

\newif\ifappendix

\def\chaplengths{{11mm,6mm,7mm,2.5mm,7mm,5mm,6mm,7mm,6.5mm}}
\def\applengths{{4mm,8.5mm,6mm}}
\renewcommand*{\chapterformat}{%
\begin{tikzpicture}[remember picture]
\coordinate (mychapanchor-\arabic{chapter});
\end{tikzpicture}
\begin{tikzpicture}[remember picture, overlay]
    \pgfmathsetmacro{\mylength}{\ifappendix\applengths[\arabic{chapter}-1]\else\chaplengths[\arabic{chapter}-1]\fi}
    \node[anchor=south east,xshift=\mylength,
          inner sep=0, outer sep=0]
          at ([yshift=1.2cm]mychapanchor-\arabic{chapter}-| current page text area.east){%
        \fontsize{10cm}{10cm}\selectfont%
        \textcolor{laccentcolor}{\thechapter}%
    };
    % alignment line
    \draw[thin] (current page text area.north east)
        -- (current page text area.south east);
\end{tikzpicture}%
}

完整 MWE:

\documentclass[BCOR=15mm, DIV=8]{scrbook}

\KOMAoptions{
    headings=twolinechapter,
    chapterprefix=false,
    numbers=noenddot
}

\usepackage{typearea}
\usepackage[utf8]{inputenc}
\usepackage{kpfonts}
\usepackage[T1]{fontenc}
\usepackage{microtype}
\usepackage{lipsum}

\usepackage{tikz}
\usetikzlibrary{
    calc,
    positioning}
\tikzset{>=latex}
\usepackage{tikzpagenodes}

\usepackage{etoolbox}
\usepackage{xstring}
\usepackage{calc}

\definecolor{laccentcolor}{HTML}{d3d3d3}


\addtokomafont{disposition}{\rmfamily}


\addtokomafont{chapter}{\scshape\LARGE}

\newif\ifappendix

\def\chaplengths{{11mm,6mm,7mm,2.5mm,7mm,5mm,6mm,7mm,6.5mm}}
\def\applengths{{4mm,8.5mm,6mm}}
\renewcommand*{\chapterformat}{%
\begin{tikzpicture}[remember picture]
\coordinate (mychapanchor-\arabic{chapter});
\end{tikzpicture}
\begin{tikzpicture}[remember picture, overlay]
    \pgfmathsetmacro{\mylength}{\ifappendix\applengths[\arabic{chapter}-1]\else\chaplengths[\arabic{chapter}-1]\fi}
    \node[anchor=south east,xshift=\mylength,
          inner sep=0, outer sep=0]
          at ([yshift=1.2cm]mychapanchor-\arabic{chapter}-| current page text area.east){%
        \fontsize{10cm}{10cm}\selectfont%
        \textcolor{laccentcolor}{\thechapter}%
    };
    % alignment line
    \draw[thin] (current page text area.north east)
        -- (current page text area.south east);
\end{tikzpicture}%
}
\renewcommand*{\chapterheadstartvskip}{\vspace*{10cm}}

\begin{document}
% 1
    \chapter{A Fancy Chapter Name to Test the Formatting}
    \lipsum[1]

% 2
    \chapter{A Fancy Chapter Name to Test the Formatting}
    \lipsum[1]

% 3
    \chapter{A Fancy Chapter Name to Test the Formatting}
    \lipsum[1]

% 4
    \chapter{A Fancy Chapter Name to Test the Formatting}
    \lipsum[1]

% 5
    \chapter{A Fancy Chapter Name to Test the Formatting}
    \lipsum[1]

% 6
    \chapter{A Fancy Chapter Name to Test the Formatting}
    \lipsum[1]

% 7
    \chapter{A Fancy Chapter Name to Test the Formatting}
    \lipsum[1]

% 8
    \chapter{A Fancy Chapter Name to Test the Formatting}
    \lipsum[1]

% 9
    \chapter{A Fancy Chapter Name to Test the Formatting}
    \lipsum[1]

    \appendix\appendixtrue

% A
    \chapter{A Fancy Chapter Name to Test the Formatting}
    \lipsum[1]
% B
    \chapter{A Fancy Chapter Name to Test the Formatting}
    \lipsum[1]
% C
    \chapter{A Fancy Chapter Name to Test the Formatting}
    \lipsum[1]
\end{document}

相关内容