I am writing a document using scrbook and I have defined a custom chapter style where I draw a large chapter number using 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}%
}
The chapter number is supposed to align to the right of the text area, but it doesn't because of the whitespace around the number that is part of the character:
The distance to the right border is different for each number. To achieve perfect alignment I would like to adjust the positioning of the chapter number on a per-chapter basis. My idea to achieve this was to define a command that returns a different length depending on the current value of \thechapter, and then use this command to define the xshift of the chapter number:
\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}%
}
I've tried multiple different ways to achieve this, but all of them until now have resulted in some kind of endless loop where my document doesn't finish building. I suspect the reason is that I am not using the right way to return a dimension from an if expression.
How can I return a dimension from a macro that changes with the current chapter? Mind that it also needs to work for non-integer numbers in the appendix. Alternatively, how can I achieve the look in the picture in some different way?
This is a current MWE of my 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}



xstringare not expandable. So you cannot use them in a context, where you need the expanded result of the test. So it maybe it would be better to define a\setoffset-command that sets a length\chapteroffsetand use that command before you need the value, e.g., before thetikzpicture. Also:\thechapterdoes not need to result in a string only (but in your example it should). – Schweinebacke Mar 06 '19 at 10:41