每次调用 \chapter 命令时更改标签颜色

每次调用 \chapter 命令时更改标签颜色

每次调用命令 \chapter{} 时,我都想更改章节标签(小矩形框)的颜色,并可能将颜色作为参数传递。

因此,我需要用参数解决的部分是命令\draw[fill=black]

这是一个可编译的示例:

\documentclass{book}
\usepackage{color}
\usepackage{tikz}
\usepackage[explicit]{titlesec}

\newcommand*\chapterlabel{}
%\renewcommand{\thechapter}{\Roman{chapter}}
\titleformat{\chapter}[display]  % type (section,chapter,etc...) to vary,  shape (eg display-type)
{\normalfont\fontsize{24}{26}\bfseries} % format of the chapter
{\gdef\chapterlabel{\thechapter\ }}     % the label 
{0pt} % separation between label and chapter-title
  {\begin{tikzpicture}[remember picture,overlay]
\node[yshift=-8cm] at (current page.north west)
  {\begin{tikzpicture}[remember picture, overlay] 
    \draw[fill=black] (0,0) rectangle(35.5mm,15mm);
    \node[anchor=north east,yshift=-7.2cm,xshift=34mm,minimum height=30mm,inner sep=0mm] at (current page.north west)
    {\parbox[top][30mm][t]{15mm}{\raggedleft $\phantom{\textrm{l}}$\color{white}\chapterlabel}};  %the black l is just to get better base-line alingement
    \node[anchor=north west,yshift=-7.2cm,xshift=37mm,text width=\textwidth,minimum height=30mm,inner sep=0mm] at (current page.north west)
          {\parbox[top][30mm][t]{\textwidth}{\color{black}#1}};
   \end{tikzpicture}
  };
 \end{tikzpicture}
  \gdef\chapterlabel{}
  } % code before the title body
 \usepackage[utf8]{inputenc}

 \title{test}

 \begin{document}

 \maketitle

 \chapter{Introduction}

 \end{document}

我尝试将参数传递给 \chapterlabel 函数,但没有成功。我还发现了一个非常相似的问题“https://tex.stackexchange.com/questions/641554/change-color-for-each-chapter”,但并没有得到真正的答案(我尝试了建议的解决方案,但没有成功)。

如果有人能帮忙我将非常感激。

答案1

我建议定义一系列名称中带有数字的颜色,然后章节将根据当前章节编号使用颜色:

\documentclass{book}
\usepackage{color}
\usepackage{tikz}
\usepackage[explicit]{titlesec}

\colorlet{chapter1}{red}
\colorlet{chapter2}{blue}

\newcommand*\chapterlabel{}
%\renewcommand{\thechapter}{\Roman{chapter}}
\titleformat{\chapter}[display]  % type (section,chapter,etc...) to vary,  shape (eg display-type)
{\normalfont\fontsize{24}{26}\bfseries} % format of the chapter
{\gdef\chapterlabel{\thechapter\ }}     % the label 
{0pt} % separation between label and chapter-title
  {\begin{tikzpicture}[remember picture,overlay]
\node[yshift=-8cm] at (current page.north west)
  {\begin{tikzpicture}[remember picture, overlay] 
    \draw[fill=chapter\the\value{chapter}] (0,0) rectangle(35.5mm,15mm);
    \node[anchor=north east,yshift=-7.2cm,xshift=34mm,minimum height=30mm,inner sep=0mm] at (current page.north west)
    {\parbox[top][30mm][t]{15mm}{\raggedleft $\phantom{\textrm{l}}$\color{white}\chapterlabel}};  %the black l is just to get better base-line alingement
    \node[anchor=north west,yshift=-7.2cm,xshift=37mm,text width=\textwidth,minimum height=30mm,inner sep=0mm] at (current page.north west)
          {\parbox[top][30mm][t]{\textwidth}{\color{black}#1}};
   \end{tikzpicture}
  };
 \end{tikzpicture}
  \gdef\chapterlabel{}
  } % code before the title body
 \usepackage[utf8]{inputenc}

 \title{test}

 \begin{document}

 \maketitle

 \chapter{Introduction}
 
  \chapter{foo}

 \end{document}

在此处输入图片描述


或者,您可以用用户定义的颜色为章节着色,并在每章之前更改其定义:

\documentclass{book}
\usepackage{color}
\usepackage{tikz}
\usepackage[explicit]{titlesec}

\colorlet{chapter}{red}


\newcommand*\chapterlabel{}
%\renewcommand{\thechapter}{\Roman{chapter}}
\titleformat{\chapter}[display]  % type (section,chapter,etc...) to vary,  shape (eg display-type)
{\normalfont\fontsize{24}{26}\bfseries} % format of the chapter
{\gdef\chapterlabel{\thechapter\ }}     % the label 
{0pt} % separation between label and chapter-title
  {\begin{tikzpicture}[remember picture,overlay]
\node[yshift=-8cm] at (current page.north west)
  {\begin{tikzpicture}[remember picture, overlay] 
    \draw[fill=chapter] (0,0) rectangle(35.5mm,15mm);
    \node[anchor=north east,yshift=-7.2cm,xshift=34mm,minimum height=30mm,inner sep=0mm] at (current page.north west)
    {\parbox[top][30mm][t]{15mm}{\raggedleft $\phantom{\textrm{l}}$\color{white}\chapterlabel}};  %the black l is just to get better base-line alingement
    \node[anchor=north west,yshift=-7.2cm,xshift=37mm,text width=\textwidth,minimum height=30mm,inner sep=0mm] at (current page.north west)
          {\parbox[top][30mm][t]{\textwidth}{\color{black}#1}};
   \end{tikzpicture}
  };
 \end{tikzpicture}
  \gdef\chapterlabel{}
  } % code before the title body
 \usepackage[utf8]{inputenc}

 \title{test}

 \begin{document}

 \maketitle

\colorlet{chapter}{green}
 \chapter{Introduction}
 
\colorlet{chapter}{orange} 
  \chapter{foo}

 \end{document}

相关内容