
我正在使用 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 表達式傳回維度。
如何從巨集中返回隨當前章節更改的尺寸?請注意,它還需要適用於附錄中的非整數。或者,我如何以不同的方式實現圖片中的外觀?
這是我目前狀態的 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}
% 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
根據土撥鼠和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}%
}
完整的 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}