
Estoy escribiendo un documento usando scrbook y he definido un estilo de capítulo personalizado donde dibujo un número de capítulo grande 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}%
}
Se supone que el número del capítulo debe alinearse a la derecha del área de texto, pero no es así debido al espacio en blanco alrededor del número que forma parte del carácter:
La distancia al borde derecho es diferente para cada número. Para lograr una alineación perfecta, me gustaría ajustar la posición del número de capítulo por capítulo. Mi idea para lograr esto fue definir un comando que devuelva una longitud diferente dependiendo del valor actual de \thechapter
y luego usar este comando para definir el xshift
número de 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}%
}
He probado varias formas diferentes de lograr esto, pero todas hasta ahora han resultado en una especie de bucle sin fin donde mi documento no termina de compilarse. Sospecho que la razón es que no estoy usando la forma correcta de devolver una dimensión a partir de una expresión if.
¿Cómo puedo devolver una dimensión de una macro que cambia con el capítulo actual? Tenga en cuenta que también debe funcionar para números no enteros en el apéndice. Alternativamente, ¿cómo puedo lograr el aspecto de la imagen de alguna manera diferente?
Este es un MWE actual de mi estado:
\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}
Respuesta1
Podrías simplemente almacenar los cambios en una matriz, que podrías usar. Dado que está cargando tikzpagenodes
, también sugeriría utilizar ese paquete.
\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}
Respuesta2
Puedes utilizar \int_case:nnF
desde 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}
Si desea ocuparse del apéndice, agregue un "comando variable". Quizás scrbook
tenga un condicional incorporado para probar si está en la parte del apéndice. Si es así, no debería resultar difícil utilizarlo en lugar del casero \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}
Respuesta3
Según las respuestas de marmot y egreg, esto es lo que terminé usando:
\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}