섹션 번호에 해당하는 목차의 색상 섹션 항목

섹션 번호에 해당하는 목차의 색상 섹션 항목

섹션 헤더의 색상이 주기적으로 바뀌는 문서를 조판합니다. 해당 섹션의 색상과 일치하도록 목차의 섹션 항목 색상을 지정하고 싶습니다.

이 작업을 수행하기 위해 \setkomafont{sectioning}목차에 색상 명령을 사용하고 "주입"합니다. 후자가 toc 항목을 오용하는 것 같은 느낌이 들긴 하지만 이것은 예상대로 작동합니다. \setkomafont{sectionentry}그리고 \setkomafont{sectionentrypagenumber}모두 동일하게 색칠하므로 작동하지 않습니다(물론 문서의 동일한 섹션에 있으므로).

문제는 hyperref를 사용할 때 Token not allowed in a PDF string이러한 "incected" 명령이 PDF 파일의 목차에 들어갈 때 경고 메시지가 나타난다는 것입니다. 더 큰 문제는 하이퍼참조가 재정의되면서 \contentsline페이지 번호의 색상이 사라진다는 것입니다. \DeclareTOCStyleEntry정확한 색상을 결정하는 데 필요한 섹션 수를 모르기 때문에 어쨌든 도움이 되지 않습니다. 그럼에도 불구하고 하이퍼참조 없이 올바르게 작동하려면 필요합니다.

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}색상과 관련하여 텍스트와 페이지 번호가 동일한 그룹에 속한다고 가정합니다. 그러나 하이퍼참조는 당연히 링크에 대한 중괄호와 그룹화를 추가해야 합니다. 전역 변수에 색상을 저장한 다음 페이지 번호에서 반복합니다.

\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}

관련 정보