目次内のセクション番号に対応するセクションエントリを色分けする

目次内のセクション番号に対応するセクションエントリを色分けする

セクション ヘッダーの色が定期的に変わる文書をタイプセットします。対応するセクションの色に合わせて、目次のセクション エントリの色も変更したいと考えています。

これを実現するために、私は\setkomafont{sectioning}toc にカラー コマンドを「挿入」します。これは期待どおりに機能しますが、後者は toc エントリの誤用の一種であるような気がします。\setkomafont{sectionentry}また、\setkomafont{sectionentrypagenumber}すべてが同じ色になるので機能しません (もちろん、ドキュメントの同じセクションにあるため)。

問題は、hyperref を使用すると、Token not allowed in a PDF stringこれらの「影響を受ける」コマンドが PDF ファイルの目次に侵入するため、警告が表示されることです。さらに問題なのは、hyperref が\contentslineページ番号の色を再定義すると、ページ番号の色がなくなることです。\DeclareTOCStyleEntry正しい色を決定するために必要なセクションの番号がわからないため、いずれにしても役に立ちません。それでも、hyperref なしで正しく色を設定するには、それが必要です。

MWE は次のとおりです。

\documentclass{scrartcl}
\usepackage{xcolor}
\usepackage{blindtext}
\usepackage{arrayjobx}
\usepackage{pgf}
\usepackage{hyperref}

\colorlet{myblue}{blue!60!green!90!black}
\colorlet{myred}{red!80!black}
\colorlet{mygreen}{green!40!black}
\colorlet{default}{black}

\newarray\structurecolors
\readarray{structurecolors}{mygreen&myblue&myred}

\makeatletter
\newcommand{\structurecolor}[1][\value{section}]{%
  \pgfmathparse{int(mod(#1,\total@structurecolors) + 1)}%
  \checkstructurecolors(\pgfmathresult)%
  \color{\cachedata}%
  \ifnum#1=0%
    \color{black}
  \fi
}
\makeatother

\setkomafont{sectioning}{\sffamily\bfseries\structurecolor}

\renewcommand{\addsectiontocentry}[2]{%
  \IfArgIsEmpty{#1}{%
    \addtocentrydefault{section}{}{#2}
  }{%
    \addtocentrydefault{section}{\structurecolor[#1]#1}{\protect\structurecolor[#1]#2}
  }
}

\DeclareTOCStyleEntry[pagenumberformat={\sffamily\bfseries}]{default}{section}


\begin{document}
  \tableofcontents
  \Blinddocument
  \Blinddocument
\end{document}

tocloftkoma-script では tocloft の使用が非推奨となっているため、このパッケージの使用は最後の手段にすぎないことを述べておきます。

\usepackage{hyperref}目的の出力を表示するにはコメントを解除してください。ご協力やご提案をいただければ幸いです。

答え1

まあ、「トークンは許可されていません」は、いつものように のように回避できます。\texorpdfstringたとえば、 のように\texorpdfstring{\protect\structurecolor[#1]#2}{#2}なります。色に関して: テキストとページ番号が同じグループにあることを前提としています。しかし、hyperref は当然、リンクに中括弧とグループ化を追加する必要があります。色を何らかのグローバル変数に格納し、ページ番号でそれを繰り返します。

\documentclass{scrartcl}
\usepackage{xcolor}
\usepackage{blindtext}
\usepackage{arrayjobx}
\usepackage{pgf}
\usepackage{hyperref}

\colorlet{myblue}{blue!60!green!90!black}
\colorlet{myred}{red!80!black}
\colorlet{mygreen}{green!40!black}
\colorlet{default}{black}

\newarray\structurecolors
\readarray{structurecolors}{mygreen&myblue&myred}

\makeatletter
\newcommand{\structurecolor}[1][\value{section}]{%
  \pgfmathparse{int(mod(#1,\total@structurecolors) + 1)}%
  \checkstructurecolors(\pgfmathresult)%
  \color{\cachedata}%
  \global\let\currentstructurecolor\cachedata
  \ifnum#1=0%
    \color{black}
  \fi
}
\makeatother

\setkomafont{sectioning}{\sffamily\bfseries\structurecolor}

\renewcommand{\addsectiontocentry}[2]{%
  \IfArgIsEmpty{#1}{%
    \addtocentrydefault{section}{}{#2}
  }{%
    \addtocentrydefault{section}{\structurecolor[#1]#1}{\texorpdfstring{\protect\structurecolor[#1]#2}{#2}}
  }
}


\DeclareTOCStyleEntry[pagenumberformat={\sffamily\bfseries\color{\currentstructurecolor}}]{default}{section}


\begin{document}
\showoutput
  \tableofcontents
  \section{a}
  \section{b}
 % \Blinddocument
 % \Blinddocument
\end{document}

ここに画像の説明を入力してください

答え2

\addtocontents{toc}{}の内部でを使用すると、\renewcommand{\addsectiontocentry}{...}PDF TOC にはまったく影響しません。\structurecolor[#1]の内部でを呼び出す必要はありません\addtocentrydefault。 を使用して\begingroup ... \endgroup変更をローカルに反映します。 この方法も\DeclareTOCStyleEntry不要になりました。

\renewcommand{\addsectiontocentry}[2]{%
  \IfArgIsEmpty{#1}{%
    \addtocentrydefault{section}{}{#2}
  }{%
    \addtocontents{toc}{\begingroup\structurecolor[#1]}
    \addtocentrydefault{section}{#1}{#2}
    \addtocontents{toc}{\endgroup}
  }
}

完全なMWE:

\documentclass{scrartcl}
\usepackage{xcolor}
\usepackage{blindtext}
\usepackage{arrayjobx}
\usepackage{pgf}
\usepackage{hyperref}

\colorlet{myblue}{blue!60!green!90!black}
\colorlet{myred}{red!80!black}
\colorlet{mygreen}{green!40!black}
\colorlet{default}{black}

\newarray\structurecolors
\readarray{structurecolors}{mygreen&myblue&myred}

\makeatletter
\newcommand{\structurecolor}[1][\value{section}]{%
  \pgfmathparse{int(mod(#1,\total@structurecolors) + 1)}%
  \checkstructurecolors(\pgfmathresult)%
  \color{\cachedata}%
  \ifnum#1=0%
    \color{black}
  \fi
}
\makeatother

\setkomafont{sectioning}{\sffamily\bfseries\structurecolor}
\setkomafont{sectionentry}{\sffamily\bfseries}

\renewcommand{\addsectiontocentry}[2]{%
  \IfArgIsEmpty{#1}{%
    \addtocentrydefault{section}{}{#2}
  }{%
    \addtocontents{toc}{\begingroup\structurecolor[#1]}
    \addtocentrydefault{section}{#1}{#2}
    \addtocontents{toc}{\endgroup}
  }
}

\begin{document}
  \tableofcontents
  \section{a}
  \section{b}
\end{document}

関連情報