Macro retornando um comprimento dependendo do capítulo atual

Macro retornando um comprimento dependendo do capítulo atual

Estou escrevendo um documento usando scrbook e defini um estilo de capítulo personalizado onde desenho um grande número de capítulo usando 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}%
}

O número do capítulo deve estar alinhado à direita da área de texto, mas isso não acontece devido ao espaço em branco ao redor do número que faz parte do caractere:

insira a descrição da imagem aqui

A distância até a borda direita é diferente para cada número. Para obter um alinhamento perfeito, gostaria de ajustar o posicionamento do número do capítulo por capítulo. Minha ideia para conseguir isso foi definir um comando que retornasse um comprimento diferente dependendo do valor atual de \thechaptere então usar este comando para definir o xshiftnúmero do capítulo:

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

Eu tentei várias maneiras diferentes de conseguir isso, mas todas elas até agora resultaram em algum tipo de loop interminável onde meu documento não termina de ser construído. Suspeito que o motivo seja que não estou usando a maneira correta de retornar uma dimensão de uma expressão if.

Como posso retornar uma dimensão de uma macro que muda com o capítulo atual? Lembre-se de que também precisa funcionar para números não inteiros no apêndice. Alternativamente, como posso conseguir a aparência da imagem de uma maneira diferente?

Este é um MWE atual do meu 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}

Responder1

Você poderia simplesmente armazenar as mudanças em uma matriz, que poderia usar. Já que você está carregando tikzpagenodes, eu também sugeriria usar esse pacote.

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

insira a descrição da imagem aqui

Responder2

Você pode usar \int_case:nnFde 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}

insira a descrição da imagem aqui

Se você quiser cuidar do apêndice adicione um “comando variável”. Talvez scrbooktenha uma condicional integrada para testar se está na parte do apêndice. Nesse caso, não deve ser difícil usá-lo em vez do caseiro \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}

Responder3

Com base nas respostas de marmot e egreg, acabei usando isso:

\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 completo:

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

informação relacionada