0

I am writing a LaTeX class that provides general layout and design features. Elements like the color scheme should be user-defined in the main document. However, the class is loaded before their definition in the main document.

How to pass latex commands like color definitions to the documentclass?

MWE Class file:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{K}[2018/05/03 Example]
\LoadClass{book}
\RequirePackage{xcolor}
\definecolor{somecolor}{rgb}{1,0.3,0.1}
% How to define this color in the main document?
\colorlet{examplecolor}{somecolor} % this is necessary
\renewcommand\thechapter{\textcolor{examplecolor}{\arabic{chapter}}}

MWE main document:

\documentclass{K}
% Ideally user-specific color definitions should go here!
% But they are already required by class K before,
% so \definecolor{somecolor}{rgb}{1,0.3,0.1} here does not work
\begin{document}
\chapter{Example chapter}
Example text
\newpage 
Example text
\end{document}

Key-value approaches probably don't work, because neither is a command a simple value, nor can I use \definecolor in the first line of the latex document before including the xcolor package. Are there alternatives to late-definitions? (Using later defined macro values)

  • If I add \definecolor{examplecolor}{rgb}{0,0,1} in the document preamble, I get the chapter number colored blue. On the other hand, it's not recommended to add formatting instructions to \thechapter. – egreg May 3 '18 at 9:58
  • 1
    You know what the more complex things are, I don't. Closing as “too broad”. – egreg May 3 '18 at 10:15
  • 1
    Rule of thumb: if the user has easy access to the class, he will fumble with the class. It doesn't matter how many hooks for customisation you provide. – Johannes_B May 3 '18 at 10:40
  • 1
    I speak from experience, they will modify the class file, if they can reach it easily. Trial and error is easier than common sense. And yes, the users I am talking about are researchers as well. – Johannes_B May 3 '18 at 13:29
  • 1
    One way of having a customizable system: key-value setup. There are some questions on this site showing how to do this. – TeXnician May 3 '18 at 13:39
0

One solution for defining colors and other commands outside of the class would be a preamble file that can be included into the class:

class file:

...
\RequirePackage{xcolor}
\input{preamble}
\colorlet{examplecolor}{somecolor}
...

preamble file:

\definecolor{somecolor}{rgb}{0,0,1}

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.