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:

\ProvidesClass{K}[2018/05/03 Example]
% How to define this color in the main document?
\colorlet{examplecolor}{somecolor} % this is necessary

MWE main document:

% 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
\chapter{Example chapter}
Example text
Example text

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

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:


preamble file:


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.