1

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)

Martin
  • 842
  • 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 03 '18 at 09:58
  • Sry those were artifacts of the MWE. I changed the code now to actually using the color (now called somecolor) in the class by \colorlet. Also, formatting \thechapter is only a simple example to illustrate my problem, I don't do it in the real code. There are other, more complex things going on in my template, which are beyond an MWE, so it'll be great to focus on the general problem rather than the content of the MWE. – Martin May 03 '18 at 10:07
  • 1
    You know what the more complex things are, I don't. Closing as “too broad”. – egreg May 03 '18 at 10:15
  • It is very specific: how to define something in the main document, that is used (e.g. by colorlet) inthe document class? The many variants of how things are used in the documentclass are totally unimportant, don't you think so? And even if they are not, then I have provided the concrete example of colorlet at least. – Martin May 03 '18 at 10:19
  • 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 03 '18 at 10:40
  • I'd argue it does matter, as I wrote the target group are non-expert users. A growing community is good for the reputation of Latex, and Latex is good for a growing community (in this case: researchers). I am trying to boil it down to make the first steps easier for researchers. And also to make this template widely applicable. Non-programmers don't have time to fumble with the code, they want a quick and easy solution. – Martin May 03 '18 at 10:53
  • 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 03 '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 03 '18 at 13:39
  • @Martin: I would avoid setting the colour as part of \thechapter, otherwise that would appear in the chapter title, all references as well as the running heads. – Werner May 03 '18 at 17:45

1 Answers1

1

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}
Martin
  • 842