8

I'm trying to pass a RGB colour code as an option to my custom-class.cls, so I can change the colour of a header bar and the colour of my titles in the main-doc.tex. The default option does work, but when I try to pass a different colour it does not work.

Code

Currently I have the following (based on this):

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

\begin{document}
\header

\section*{Title test}
Test 

\end{document}

And the 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}{}

Result with a default value

default option

Result with a custom value

When I change in main_doc.tex the following line:

\documentclass[]{custom-class}

into:

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

I get the following errors:

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}

and one warning:

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

I've tried https://tex.stackexchange.com/a/145454/140011 as Marijn suggested and adding \RequirePackage{kvoptions-patch} does the trick. As explained in the package readme (p. 11) it addresses the following:

Latex's system of package/class options has some severe limitations that especially affects the value part if options are used as pair of key and value.

According to their example in your current setting changecolor={206, 23, 21}acutally becomes changecolor=2062321.

An update to the file:

% 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}{}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.