Makro, das eine vom aktuellen Kapitel abhängige Länge zurückgibt

Makro, das eine vom aktuellen Kapitel abhängige Länge zurückgibt

Ich schreibe ein Dokument mit Scrbook und habe einen benutzerdefinierten Kapitelstil definiert, in dem ich mit Tikz eine große Kapitelnummer zeichne:

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

Die Kapitelnummer sollte rechts neben dem Textbereich ausgerichtet sein, dies ist jedoch aufgrund des Leerzeichens um die Nummer, das Teil des Zeichens ist, nicht der Fall:

Bildbeschreibung hier eingeben

Der Abstand zum rechten Rand ist für jede Nummer unterschiedlich. Um eine perfekte Ausrichtung zu erreichen, möchte ich die Positionierung der Kapitelnummer für jedes Kapitel einzeln anpassen. Meine Idee dazu war, einen Befehl zu definieren, der je nach aktuellem Wert eine andere Länge zurückgibt \thechapter, und dann diesen Befehl zu verwenden, um die xshiftder Kapitelnummer zu definieren:

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

Ich habe mehrere verschiedene Möglichkeiten ausprobiert, um dies zu erreichen, aber alle haben bisher zu einer Art Endlosschleife geführt, in der mein Dokument nicht fertig erstellt wird. Ich vermute, der Grund liegt darin, dass ich nicht die richtige Methode verwende, um eine Dimension aus einem if-Ausdruck zurückzugeben.

Wie kann ich aus einem Makro eine Dimension zurückgeben, die sich mit dem aktuellen Kapitel ändert? Dabei muss es auch für nicht-ganzzahlige Zahlen im Anhang funktionieren. Oder wie kann ich die Optik im Bild anders erreichen?

Dies ist ein aktuelles MWE meines Status:

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

Antwort1

Sie könnten die Verschiebungen einfach in einem Array speichern, das Sie verwenden könnten. Da Sie laden tikzpagenodes, würde ich auch vorschlagen, dieses Paket zu verwenden.

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

Bildbeschreibung hier eingeben

Antwort2

Sie können \int_case:nnFFolgendes verwenden 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}

Bildbeschreibung hier eingeben

Wenn Sie sich um den Anhang kümmern möchten, fügen Sie einen „Variablenbefehl“ hinzu. Vielleicht scrbookhat dieser eine eingebaute Bedingung, um zu testen, ob er sich im Anhang befindet. Wenn ja, sollte es nicht schwierig sein, ihn anstelle des selbst erstellten zu verwenden \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}

Antwort3

Basierend auf den Antworten von Marmot und Egreg habe ich letztendlich Folgendes verwendet:

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

Vollständiges 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}

verwandte Informationen