代码

代码

我正在尝试将 RGB 颜色代码作为选项传递给我的custom-class.cls,以便我可以更改标题栏的颜色和标题的颜色main-doc.tex。默认选项确实有效,但当我尝试传递不同的颜色时它不起作用。

代码

目前我有以下内容(基于):

% main-doc.tex
\documentclass[]{custom-class}

\begin{document}
\header

\section*{Title test}
Test 

\end{document}

还有custom-class.cls

% custom-class.cls
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{custom-class}[2017/07/25 My custom class]
\LoadClass[a4paper]{article}

% Load required packages
\RequirePackage{titlesec} % To alter the style and spacing of titles
\RequirePackage{xcolor} % Enables the use of colours
\RequirePackage{tikz} % Draw things, needed for the header
\RequirePackage{xkeyval} % Pass options to sutom-class

% \define@key[<prefix>]{<family>}{<key>}[<default>]{<function>}
\define@key{custom-class.cls}{changecolor}[102, 204, 51]{%
    \definecolor{maincolor}{RGB}{#1}%
}

\ExecuteOptionsX{changecolor}
\ProcessOptionsX%

% Coloured header
\newcommand{\header}{%
  \begin{tikzpicture}[remember picture, overlay]
    % Colored bar on top of the page
    \node [below right, fill=maincolor, minimum height=4 cm, minimum width=\paperwidth] at (current page.north west) {};
  \end{tikzpicture}
  \vspace{1 em}
} 

% Change format of the section title
\titleformat{\section}{\color{maincolor}\Large}{\thesection}{0em}{}

具有默认值的结果

默认选项

具有自定义值的结果

当我更改main_doc.tex以下行时:

\documentclass[]{custom-class}

进入:

\documentclass[changecolor={206, 23, 21}]{custom-class}

我收到以下错误:

line 18: Missing \begin{document}.
line 18: You can't use `macro parameter character #' in horizontal mode.
line 18: Paragraph ended before \reserved@a was complete.
line 4: \RequirePackage or \LoadClass in Options Section. \begin{document}

还有一个警告:

Unused global option(s): [changecolor={206, 23, 21}].

答案1

我试过了https://tex.stackexchange.com/a/145454/140011正如 Marijn 所建议的,添加\RequirePackage{kvoptions-patch}即可解决问题。正如所解释的在包自述文件中(第 11 页)它讨论了以下内容:

Latex 的包/类选项系统有一些严重的限制,如果选项用作键和值对,则尤其会影响值部分。

根据他们的例子,您当前的设置changecolor={206, 23, 21}实际上变成了changecolor=2062321

文件更新:

% custom-class.cls
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{custom-class}[2017/07/25 My custom class]
\LoadClass[a4paper]{article}

% Load required packages
\RequirePackage{titlesec} % To alter the style and spacing of titles
\RequirePackage{xcolor} % Enables the use of colours
\RequirePackage{tikz} % Draw things, needed for the header
\RequirePackage{kvoptions-patch}
\RequirePackage{xkeyval} % Pass options to sutom-class

% \define@key[<prefix>]{<family>}{<key>}[<default>]{<function>}
\define@key{custom-class.cls}{changecolor}[102, 204, 51]{%
    \definecolor{maincolor}{RGB}{#1}%
}

\ExecuteOptionsX{changecolor}
\ProcessOptionsX%

% Coloured header
\newcommand{\header}{%
  \begin{tikzpicture}[remember picture, overlay]
    % Colored bar on top of the page
    \node [below right, fill=maincolor, minimum height=4 cm, minimum width=\paperwidth] at (current page.north west) {};
  \end{tikzpicture}
  \vspace{1 em}
} 

% Change format of the section title
\titleformat{\section}{\color{maincolor}\Large}{\thesection}{0em}{}

相关内容