我正在尝试制作一个包含多个“主题”的文档。这涉及根据当前部分更改标题、部分名称和标题的颜色。我想要使用的颜色是自定义颜色。
现在,我知道如何使整个文档都相同,但我试图找到一种方法,根据当前部分值自动更改它。我认为使用这个ifthen
包会很容易,因为它似乎是为此而制作的(我可以看到部分是否等于一个值)。然而,事情并没有按预期进行。我在下面有一个 MWE。在这里,为了清楚起见,我只更改了子部分名称。我试图使子部分第一节红色和小节第二节蓝色。然而,一切是蓝色的。我对标题做了类似的事情,但没有任何效果。有人能帮我指出正确的方向吗?
\documentclass[twoside]{article}
% For blind text
\usepackage[english]{babel}
\usepackage{blindtext}
% Here begins my attempt
\usepackage{ifthen}
\usepackage{titlesec}
% Coloring the section names (didn't mess with this, but I am using this as the basis of coloring the section and subsection names)
\titleformat{\section}{\LARGE\normalfont\center\color{blue}}{\thesection}{1em}{}
% My attempted ifthenelse statement. I put the if, then, and else arguments on different lines for clarity
\ifthenelse
{\equal{\thesection}{Section One}}
{\titleformat{\subsection}{\Large\normalfont\color{red}}{\thesubsection}{1em}{}}
{\titleformat{\subsection}{\Large\normalfont\color{blue}}{\thesubsection}{1em}{}}
% Sections begin on new page
\newcommand{\sectionbreak}{\clearpage}
% For headers
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[OR]{{\textsc{\leftmark}}\quad\quad\thepage}
\fancyhead[EL]{\thepage\quad\quad{\textsc{\leftmark}}}
\renewcommand{\sectionmark}[1]{\markboth{#1}{}}
% For colors
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\begin{document}
\section{Section One}
\subsection{Subsection One}
\blindtext[5]
\subsection{Subsection Two}
\blindtext[5]
\section{Section Two}
\subsection{Subsection One}
\blindtext[5]
\subsection{Subsection Two}
\blindtext[5]
\end{document}
答案1
也许使用颜色系列可以简化问题。\color{foo!![\thesection]}
颜色是根据部分编号来选择的。
\documentclass[twoside]{article}
% For blind text
\usepackage[english]{babel}
\usepackage{blindtext}
% Here begins my attempt
\usepackage{ifthen}
\usepackage{titlesec}
% Coloring the section names (didn't mess with this, but I am using this as the basis of coloring the section and subsection names)
\titleformat{\section}{\LARGE\normalfont\center\color{foo!![\thesection]}}{\thesection}{1em}{}
\titleformat{\subsection}{\Large\normalfont\color{foo!![\thesection]}}{\thesubsection}{1em}{}
% For headers
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[OR]{{\color{foo!![\thesection]}\textsc{\leftmark}}\quad\quad\color{foo!![\thesection]}\thepage}
\fancyhead[EL]{\color{foo!![\thesection]}\thepage\quad\quad{\textsc{\color{foo!![\thesection]}\leftmark}}}
\renewcommand{\sectionmark}[1]{\markboth{\color{foo!![\thesection]}#1}{}}
% For colors
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\definecolorseries{foo}{rgb}{step}{red}[rgb]{1,0,1}% 1/24
\resetcolorseries[2]{foo}
\begin{document}
\section{Section One}
\subsection{Subsection One}
\blindtext[2]
\clearpage
\section{Section Two}
\subsection{Subsection One}
\blindtext[2]
\end{document}
或者基于https://tex.stackexchange.com/a/370470/36296具有固定颜色的变体:
\documentclass[twoside]{article}
% For blind text
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\definecolor{zz0}{rgb}{.1,.8,.3}
\definecolor{zz1}{rgb}{.8,.2,.2}
\definecolor{zz2}{rgb}{.7,.9,.1}
\definecolor{zz3}{rgb}{.6,.4, 0}
\definecolor{zz4}{rgb}{.5,.1,.9}
\definecolor{zz5}{rgb}{.1, 0,.8}
\definecolor{zz6}{rgb}{.7,.2,.6}
\definecolor{zz7}{rgb}{.9,.3,.8}
\definecolor{zz8}{rgb}{0 ,.4,.1}
\definecolor{zz9}{rgb}{.7,.5,.2}
\definecolor{zz10}{rgb}{.1,.6,.3}
\definecolor{zz11}{rgb}{.6,.8,.5}
% Here begins my attempt
%\usepackage{ifthen}
\usepackage{titlesec}
% Coloring the section names (didn't mess with this, but I am using this as the basis of coloring the section and subsection names)
\titleformat{\section}{\LARGE\normalfont\center\color{zz\arabic{section}}}{\thesection}{1em}{}
\titleformat{\subsection}{\Large\normalfont\color{zz\arabic{section}}}{\thesubsection}{1em}{}
\newcommand{\sectionbreak}{\clearpage}
% For headers
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[OR]{{\color{zz\arabic{section}}\textsc{\leftmark}}\quad\quad\color{zz\arabic{section}}\thepage}
\fancyhead[EL]{\color{zz\arabic{section}}\thepage\quad\quad{\textsc{\color{zz\arabic{section}}\leftmark}}}
\renewcommand{\sectionmark}[1]{\markboth{\color{zz\arabic{section}}#1}{}}
\begin{document}
\section{Section}
\subsection{Subsection}
\blindtext[2]
\section{Section}
\subsection{Subsection}
\blindtext[2]
\section{Section}
\subsection{Subsection}
\blindtext[2]
\section{Section}
\subsection{Subsection}
\blindtext[2]
\section{Section}
\subsection{Subsection}
\blindtext[2]
\end{document}