我正在编写一个可以调用的类myclass
。我尝试定义以下命令来更改文本颜色(文本、部分等):
\RequirePackage{xcolor}
\newcommand{\setsectioncolor}[1]{\color[#1]}
\newcommand{\sectioncolor}{}
\newcommand{\setsubsectioncolor}[1]{\color{#1}}
\newcommand{\subsectioncolor}{}
\newcommand{\setsubsubsectioncolor}[1]{\color{#1}}
\newcommand{\subsubsectioncolor}{}
\newcommand{\settxtcolor}[1]{\color{#1}}
\newcommand{\txtcolor}{}
问题是我不知道如何定义\sectcolor
等,确实需要一些帮助。我会尝试解释我想用这些命令做什么。
我想使用以下方法重新定义部分等\sectcolor
:
\renewcommand{\section}{\@startsection {section}{1}{\z@}%
{-3.5ex \@plus -1ex \@minus -.2ex}%
{2.3ex \@plus.2ex}%
{\headingfont\Large\bfseries\sectioncolor}}
答案1
这里有一个允许您调整分段单元颜色的实现。\resetXcolor
提供了附加宏来删除任何先前的颜色定义。
\documentclass{article}
\usepackage{xcolor}
\newcommand{\setsectioncolor}[1]{\renewcommand{\sectioncolor}{\color{#1}}}
\newcommand{\sectioncolor}{}% Default is no colour
\newcommand{\resetsectioncolor}{\renewcommand{\sectioncolor}{}}% Restore (default) section colour
\newcommand{\setsubsectioncolor}[1]{\renewcommand{\subsectioncolor}{\color{#1}}}
\newcommand{\subsectioncolor}{}% Default is no colour
\newcommand{\resetsubsectioncolor}{\renewcommand{\subsectioncolor}{}}% Restore (default) subsection colour
\newcommand{\setsubsubsectioncolor}[1]{\renewcommand{\subsubsectioncolor}{\color{#1}}}
\newcommand{\subsubsectioncolor}{}% Default is no colour
\newcommand{\resetsubsubsectioncolor}{\renewcommand{\subsubsectioncolor}{}}% Restore (default) subsection colour
\makeatletter
% Taken from https://www.tug.org/svn/texlive/trunk/Master/texmf-dist/tex/latex/base/article.cls?view=co
\renewcommand\section{\@startsection {section}{1}{\z@}%
{-3.5ex \@plus -1ex \@minus -.2ex}%
{2.3ex \@plus.2ex}%
{\normalfont\Large\bfseries\sectioncolor}}
\renewcommand\subsection{\@startsection{subsection}{2}{\z@}%
{-3.25ex\@plus -1ex \@minus -.2ex}%
{1.5ex \@plus .2ex}%
{\normalfont\large\bfseries\subsectioncolor}}
\renewcommand\subsubsection{\@startsection{subsubsection}{3}{\z@}%
{-3.25ex\@plus -1ex \@minus -.2ex}%
{1.5ex \@plus .2ex}%
{\normalfont\normalsize\bfseries\subsubsectioncolor}}
\makeatother
\begin{document}
\tableofcontents
\setsectioncolor{red}
\section{A red section}
\setsubsectioncolor{green}
\subsection{A green subsection}
\setsubsubsectioncolor{blue}
\subsubsection{A blue subsubsection}
\resetsectioncolor
\section{A section}
\end{document}
注意sectsty
已经提供了类似的设置:
\documentclass{article}
\usepackage{xcolor,sectsty}
\begin{document}
\tableofcontents
\sectionfont{\color{red}}
\section{A red section}
\subsectionfont{\color{green}}
\subsection{A green subsection}
\subsubsectionfont{\color{blue}}
\subsubsection{A blue subsubsection}
\sectionfont{\color{black}}
\section{A section}
\end{document}
答案2
您必须将颜色模型传递给标签\sectioncolor
,修改MWE
如下:
\documentclass{book}
\RequirePackage{xcolor}
\newcommand{\setsectioncolor}[1]{\color[#1]}
\newcommand{\sectioncolor}{\color{cyan}}
\newcommand{\setsubsectioncolor}[1]{\color{#1}}
\newcommand{\subsectioncolor}{}
\newcommand{\setsubsubsectioncolor}[1]{\color{#1}}
\newcommand{\subsubsectioncolor}{}
\newcommand{\settxtcolor}[1]{\color{#1}}
\newcommand{\txtcolor}{}
\newcommand{\headingfont}{\Large\bfseries}%
\begin{document}
\makeatletter
\renewcommand{\section}{\@startsection {section}{1}{\z@}%
{-3.5ex \@plus -1ex \@minus -.2ex}%
{2.3ex \@plus.2ex}%
{\headingfont\sectioncolor}}
\makeatother
\section{Test}
\end{document}
我已经定义了一个新标签\headingfont
并将其放入\Large\bfseries
该标签中,因此如果您想更改字体样式,这很容易\section
...