
我排版了一個文檔,其中節標題的顏色定期變化。我想為目錄中的部分條目著色,並與相應部分的顏色相符。
為了完成此任務,我\setkomafont{sectioning}
在目錄中使用並“注入”顏色命令。這確實按預期工作,儘管我感覺後者是對目錄條目的一種濫用。\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}
我應該提到,使用該tocloft
包只是最後的手段,因為 koma-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 目錄。那你不需要\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}