以下 def 将前三个字母作为参数:
\newcounter{colorCounter}
\def\@sectioncolor#1#2#3{%
{%
\color{%
\ifcase\value{colorCounter}%
blue\or%
red\or%
orange\or%
green\or%
purple\or%
brown\else%
headercolor\fi%
} #1#2#3%
}%
\stepcounter{colorCounter}%
}
它实际上会为部分名称的前三个字母着色。我该如何让它为整个部分名称着色?
我可以把整个段落作为论据吗?
编辑
为了让它发挥作用,还必须
\renewcommand{\section}[1]{
\par\vspace{\parskip}
{%
\LARGE\headingfont\color{headercolor}%
\@sectioncolor #1%
}
\par\vspace{\parskip}
}
编辑2
梅威瑟:
\documentclass{article}
\usepackage{fontspec}
\usepackage{polyglossia}
% fonts:
\defaultfontfeatures{Scale=MatchLowercase,Mapping=tex-text}
\setmainfont{DejaVu Sans}
\setsansfont{DejaVu Sans}
\setmonofont{DejaVu Sans Mono}
\usepackage{xcolor}
\colorlet{headercolor}{gray}
\newcounter{colorCounter}
\def\@sectioncolor#1#2#3{%
{%
\color{%
\ifcase\value{colorCounter}%
blue\or%
red\or%
orange\or%
green\or%
purple\or%
brown\else%
headercolor\fi%
} #1#2#3%
}%
\stepcounter{colorCounter}%
}
\renewcommand{\section}[1]{
\par\vspace{\parskip}
{%
\LARGE\color{headercolor}%
\@sectioncolor #1%
}
\par\vspace{\parskip}
}
\begin{document}
\section{aaaaaa}
\section{bbbbbb}
\section{cccccc}
\end{document}
答案1
使用sectsty
(或者,再做一些工作titlesec
):
\documentclass{article}
\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{sectsty}
\colorlet{headercolor}{gray}
\newcounter{colorCounter}
\newcommand\sectioncolor{%
\color{%
\ifcase\value{colorCounter}%
blue\or
red\or
orange\or
green\or
purple\or
brown\else
headercolor\fi
}%
\stepcounter{colorCounter}%
% Remove the following line if you don't want to cycle
\ifnum\value{colorCounter}=7 \setcounter{colorCounter}{0}\fi
}
% in section titles \sectioncolor will be executed
\sectionfont{\sectioncolor}
% don't number sections
\setcounter{secnumdepth}{0}
\begin{document}
\section{aaaaaa}
\section{bbbbbb}
\section{cccccc}
\section{dddddd}
\section{eeeeee}
\section{ffffff}
\section{gggggg}
\section{hhhhhh}
\end{document}
答案2
哦,我解决了:
\documentclass{article}
\usepackage{fontspec}
\usepackage{polyglossia}
% fonts:
\defaultfontfeatures{Scale=MatchLowercase,Mapping=tex-text}
\setmainfont{DejaVu Sans}
\setsansfont{DejaVu Sans}
\setmonofont{DejaVu Sans Mono}
\usepackage{xcolor}
\colorlet{headercolor}{gray}
\newcounter{ColorCounter}
\newcommand\Color{
\color{%
\ifcase\value{ColorCounter}%
blue\or%
red\or%
orange\or%
green\or%
purple\or%
brown\else%
headercolor%
\fi%
}
}
\renewcommand{\section}[1]{
\par\vspace{\parskip}
{
\LARGE\Color #1
}
\par\vspace{\parskip}
\stepcounter{ColorCounter}
}
\begin{document}
\section{aaaaaa}
\section{bbbbbb}
\section{cccccc}
\end{document}