5

I'm trying to design a tex file that could store multiple colour schemes (which I would define once and for all), which I could "activate" one at a time to assign quickly a colour scheme to my document. This might not be clear so here's my code for now :

\usepackage{xcolor}
\usepackage{sectsty}

%Current scheme
\definecolor{belux}{HTML}{3366FF}
\definecolor{underbelux}{HTML}{4477EE}

%_____________________________________________________________
%Existing schemes

\begin{comment}
%orange
\definecolor{belux}{RGB}{189,79,0}
\definecolor{underbelux}{RGB}{237,127,16}

%blue
\definecolor{belux}{HTML}{3366FF}
\definecolor{underbelux}{HTML}{3366FF}

\end{comment}

%______________________________________________________________
%Color selection for each header
\chapterfont{\color{belux}} 
\sectionfont{\color{belux}}  
\subsectionfont{\color{belux}}
\subsubsectionfont{\color{underbelux}}

\newcommand{\coloritem}[1]{\item[\textcolor{belux}{[#1]}]}
\newcommand{\undercoloritem}[1]{\item[\textcolor{underbelux}{[#1]}]}

Here, you can see all my colours are named "belux" and "underbelux" (which means nothing), so that when I want to change colour schemes, I don't have to change manually the colours in the \xxxfont{\color{belux}} instructions. To do that, only the active colour scheme is uncommented (in the "Active scheme" part). The other schemes are in a commented section for easy access, and to ensure the compiler doesn't compile all of them.

The way this works for now, I have to copy and paste the scheme-specific "belux" color definition commands from the "existing schemes" part into the "current schemes part". This is not quite how I'd like it to work, for in the long term I'll probably create many other schemes.

I would like to change this so that every scheme has a defined parameter (a number or a name) which I could "call" in the beginning of this document, and which, when "called", would uncomment the \definecolor commands of this specific scheme while commenting the rest.

The way it would work would be similar to a Java switch instruction :

switch (activescheme){
    case orange : 
        \definecolor{belux}{RGB}{189,79,0}
        \definecolor{underbelux}{RGB}{237,127,16}
    case blue :
        \definecolor{belux}{HTML}{3366FF}
        \definecolor{underbelux}{HTML}{3366FF}
    default :
         do nothing
}

Well this is no correct Java but it might give you an idea of the structure I would like it to have. I would change the value of the activescheme parameter so as to unlock only one color scheme, while the others would be ignored.

I'm not familiar with \ifxxx ... \fi structures but I reckon I would have to define a boolean for every scheme and thus have many boolean definitions in the beginning of the file. Here's what I could make with my current knowledge of this structure :

\usepackage{xcolor}
\usepackage{sectsty}

\newif\ifblue
\newif\iforange

\bluetrue
\orangefalse

%_____________________________________________________________
%Existing schemes

%orange
\iforange
\definecolor{belux}{RGB}{189,79,0}
\definecolor{underbelux}{RGB}{237,127,16}
\fi

%blue
\ifblue
\definecolor{belux}{HTML}{3366FF}
\definecolor{underbelux}{HTML}{3366FF}
\fi

%______________________________________________________________
%Color selection for each header
\chapterfont{\color{belux}} 
\sectionfont{\color{belux}}  
\subsectionfont{\color{belux}}
\subsubsectionfont{\color{underbelux}}

\newcommand{\coloritem}[1]{\item[\textcolor{belux}{[#1]}]}
\newcommand{\undercoloritem}[1]{\item[\textcolor{underbelux}{[#1]}]}

Which would be unmanageable shall I create more than a few colour schemes.

So, my question is : is there another way to use \ifxxx ... \fi structures that would allow me to create the structure I'd like to have or are there other structures that could do that ?

Every help will be appreciated, and sorry for the very long question ;)

3

As is explained in page 1 from comment documentation, you can define as much as commentable environments as you want, just include \includecomment{name of environment} or \excludecomment{name of environment} to select or unselect corresponding lines in your document.

With them you can declare environments for any color combination

\begin{orange}
%orange
\definecolor{belux}{RGB}{189,79,0}
\definecolor{underbelux}{RGB}{237,127,16}
\end{orange}

and use or not with \includecomment{orange} or \excludecomment{orange}.

\documentclass{book}
\usepackage{xcolor}
\usepackage{sectsty}
\usepackage{comment}

%Current scheme
\definecolor{belux}{HTML}{FF3366}
\definecolor{underbelux}{HTML}{EE4477}

%_____________________________________________________________
%Existing schemes

%leave one uncommented
%\includecomment{orange}
\excludecomment{orange}

%leave one uncommented
%\includecomment{blue}
\excludecomment{blue}

\begin{orange}
%orange
\definecolor{belux}{RGB}{189,79,0}
\definecolor{underbelux}{RGB}{237,127,16}
\end{orange}

\begin{blue}
%blue
\definecolor{belux}{HTML}{3366FF}
\definecolor{underbelux}{HTML}{3366FF}
\end{blue}

%______________________________________________________________
%Color selection for each header
\chapterfont{\color{belux}} 
\sectionfont{\color{belux}}  
\subsectionfont{\color{belux}}
\subsubsectionfont{\color{underbelux}}

\newcommand{\coloritem}[1]{\item[\textcolor{belux}{[#1]}]}
\newcommand{\undercoloritem}[1]{\item[\textcolor{underbelux}{[#1]}]}

\begin{document}

\chapter{First chapter}
\section{First section}
\begin{itemize}
\coloritem{A}
\coloritem{B}
\end{itemize}
\end{document}

Playing with several combinations of \include...-\exclude... commands in previous code we obtain:

enter image description hereenter image description hereenter image description here

Update:

After diving a little bit in CTAN I've found a package called boolexpr which defines a switch conditional. I didn't know this package and I don't know if there exist better options.

In this case, every color set can be associated to an integer defined with a counter.

\newcounter{myColorSelector} 
\setcounter{myColorSelector}{5}

\switch[\value{myColorSelector}]
\case{=1}%orange
\definecolor{belux}{RGB}{189,79,0}
\definecolor{underbelux}{RGB}{237,127,16}
\case{=2}%blue
\definecolor{belux}{HTML}{3366FF}
\definecolor{underbelux}{HTML}{3366FF}
\otherwise%default
\definecolor{belux}{HTML}{FF3366}
\definecolor{underbelux}{HTML}{EE4477}
\endswitch

The complete code

\documentclass{book}
\usepackage{xcolor}
\usepackage{sectsty}
\usepackage{boolexpr}

%_____________________________________________________________
%Existing schemes

\newcounter{myColorSelector} \setcounter{myColorSelector}{5}

\switch[\value{myColorSelector}]
\case{=1}%orange
\definecolor{belux}{RGB}{189,79,0}
\definecolor{underbelux}{RGB}{237,127,16}
\case{=2}%blue
\definecolor{belux}{HTML}{3366FF}
\definecolor{underbelux}{HTML}{3366FF}
\otherwise%default
\definecolor{belux}{HTML}{FF3366}
\definecolor{underbelux}{HTML}{EE4477}
\endswitch

%______________________________________________________________
%Color selection for each header
\chapterfont{\color{belux}} 
\sectionfont{\color{belux}}  
\subsectionfont{\color{belux}}
\subsubsectionfont{\color{underbelux}}

\newcommand{\coloritem}[1]{\item[\textcolor{belux}{[#1]}]}
\newcommand{\undercoloritem}[1]{\item[\textcolor{underbelux}{[#1]}]}

\begin{document}

\chapter{First chapter}
\section{First section}
\begin{itemize}
\coloritem{A}
\coloritem{B}
\end{itemize}
\end{document}
  • Thank you for your answer. I didn't know of the custom comment environments you talk of, but I surely will use them instead of the basic comment environment. However, as I stated in the question, I'd like to know if a cleaner approach exists (the java thing), so that when I create, say, 10 schemes, I don't have to have 20 lines of \includecomment / \excludecomment commands at the beginning of my code. But if that approach doesn't exist, your solution fits me well ;) – oowekyala May 7 '15 at 17:19
  • @lepiment I've updated the answer with a switch environment from boolexpr package – Ignasi May 7 '15 at 17:53
3

I would approach this problem using a key-value interface to do most of the heavy lifting. The advantages of this method are that you can define new schemes anywhere, and a single document can use more than one scheme if desired (respecting grouping/scope).

There are several keyval packages, and the code can get even more fancy than what I show below, but here's a simple example of what might be done. The macro \definescheme is used to create a scheme, with usage:

\definescheme{<scheme-name>}{<belux-model>}{<belux-spec>}{<underbelux-model>}{<underbelux-spec>}

After a scheme <scheme-name> has been defined, it can be selected anywhere in the document with the macro \setscheme{<scheme-name>}. The scheme selection will apply from the point it is set to the end of the group.

This is illustrated in the simple example below. There is no error-checking code, and any value set with a key (for example, \setscheme{orange=5}, where 5 is the value) is silently discarded. The code could be extended to use a value for some special purpose if desired.

Here's the code:

\documentclass{book}
\usepackage{xcolor}
\usepackage{sectsty}
\usepackage{keyval}

%key-handling interface
\makeatletter
\def\definescheme#1#2#3#4#5{%
  \define@key{myschemes}{#1}[]{%
    \definecolor{belux}     {#2}{#3}%
    \definecolor{underbelux}{#4}{#5}%
  }%
}%
\makeatother
\def\setscheme#1{\setkeys{myschemes}{#1}}

%Default scheme
\definecolor{belux}{HTML}{3366FF}
\definecolor{underbelux}{HTML}{4477EE}

%_____________________________________________________________
%Existing schemes
\definescheme{orange}{RGB}{189,79,0}{RGB}{237,127,16}
\definescheme{blue}{HTML}{3366FF}{HTML}{3366FF}
\definescheme{red}{RGB}{255,0,0}{RGB}{237,0,0}

%______________________________________________________________
%Color selection for each header
\chapterfont{\color{belux}} 
\sectionfont{\color{belux}}  
\subsectionfont{\color{belux}}
\subsubsectionfont{\color{underbelux}}

\newcommand{\coloritem}[1]{\item[\textcolor{belux}{[#1]}]}
\newcommand{\undercoloritem}[1]{\item[\textcolor{underbelux}{[#1]}]}

\begin{document}
\chapter{First chapter}
\section{First section}
\begin{itemize}
\coloritem{A} Test 
{ % scheme setting can obey grouping if desired
  \setscheme{orange}
  \coloritem{B} Test
}
\coloritem{C} Test
\end{itemize}
\setscheme{red}
\section{Red section}
\subsection{Also red}
\setscheme{blue}
\subsubsection{Now blue}
\end{document}

And the corresponding output:

enter image description here

  • I didn't know this package, and I have to say that, at first, what you've done here was far out of my understanding. But after going through keyval documentation, I think this may be a more flexible solution than the one I'd previously reckoned, so I'll use it from now on. Thank you for your answer ! – oowekyala May 8 '15 at 22:12
  • @lepiment you're welcome! The keyval package is one of the more basic packages for this sort of thing. Some other related packages have more extensive interfaces that you might be interested in. – Paul Gessler May 8 '15 at 22:15
1

I'm putting this as an answer despite it's brevity. Sometimes the least convoluted thing is the best thing, and the most extensible thing.

I would just create a set of files, one for each scheme. In each file define the colors you want.

Then just include a line in your file

\input{colorschemepurple.tex}

  • That would lead to creating a lot of files, plus you can't modify your schemes on the go, in the same file (have to open each file to modify the colours). In my opinion, the solution given by Mr. Ignasi is a better alternative to this, it allows the management of multiple schemes quickly and in a centralized manner. – oowekyala May 7 '15 at 19:06
  • But this is "centralized" - all your schemes would be in a common folder accessible to all your projects. – Aubrey Blumsohn May 8 '15 at 8:40
1

This is what macros are for. ;-)

\documentclass{report}

\usepackage[paperwidth=8cm,paperheight=13cm]{geometry}

\usepackage{xcolor}
\usepackage{sectsty}

\newcommand{\currentscheme}{%
  \definecolor{belux}{HTML}{3366FF}%
  \definecolor{underbelux}{HTML}{4477EE}%
}

\newcommand{\orangescheme}{%
  \definecolor{belux}{RGB}{189,79,0}%
  \definecolor{underbelux}{RGB}{237,127,16}%
}

\newcommand{\bluescheme}{%
  \definecolor{belux}{HTML}{3366FF}%
  \definecolor{underbelux}{HTML}{3366FF}%
}

%Color selection for each header
\chapterfont{\color{belux}} 
\sectionfont{\color{belux}}  
\subsectionfont{\color{belux}}
\subsubsectionfont{\color{underbelux}}

\newcommand{\coloritem}[1]{\item[\textcolor{belux}{[#1]}]}
\newcommand{\undercoloritem}[1]{\item[\textcolor{underbelux}{[#1]}]}

\currentscheme

\begin{document}

\chapter{Title}

\section{Title}
\subsection{Title}
\subsubsection{Title}

%%% just for testing

\orangescheme

\chapter{Title}

\section{Title}
\subsection{Title}
\subsubsection{Title}

\end{document}

enter image description here

  • You are right, I could have done that even with my poor level in LaTeX. I should have thought of it. Nevertheless, I think Mr. Gessler provided a solution that seems... more comfortable. In practice, I think very little distinguishes the keyval posibility from your code but the relative complexity of the former and its enhanced practicalness. The file in which I keep my schemes is much cleaner I think; But thank you for your answer ! – oowekyala May 9 '15 at 19:02

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.