Макрос, возвращающий длину в зависимости от текущей главы

Макрос, возвращающий длину в зависимости от текущей главы

Я пишу документ с помощью 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.

Как вернуть измерение из макроса, который меняется с текущей главой? Помните, что он также должен работать для нецелых чисел в приложении. Или как можно добиться вида на картинке каким-то другим способом?

Это текущая оценка моего статуса:

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

Полная МВЭ:

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

Связанный контент