現在の章に応じて長さを返すマクロ

現在の章に応じて長さを返すマクロ

私は 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

marmot と 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}

関連情報