Document ClassesPassing rgb colour syntax

Information and discussion about specific document classes and how to create your own document classes.
Post Reply
Vaite
Posts: 2
Joined: Thu Oct 12, 2023 12:19 am

Passing rgb colour syntax

Post by Vaite »

I want to pass a colour scheme using the syntax

Code: Select all

[red]
[rgb][0.98,41.0,0.08]
[RGB][249,104,21]

Two optional arguments are used to pass the color specifications to this. But such implementation results in

Code: Select all

Package xcolor Error: Undefined color `RGB'.
Here are the details

Code: Select all

\documentclass[a4paper,12pt]{article}

\usepackage{xcolor}

\ExplSyntaxOn

\NewDocumentEnvironment{that}{O{orange}ommm}
  { 
    #3 #4 #5
    \color{#1}{#2}
  }
  {}

\NewDocumentEnvironment{this}{O{orange}omm}
  {
    \begin{that}[#1][#2]{Something}{#3}[#4]
  }
  {
     \end{that}
  }

\ExplSyntaxOff

\begin{document}

\begin{this}[red]{Name}{Surname}
  Some Text.
\end{this}

\begin{this}[RGB][249,104,21]{Name}{Surname}
  Some Text.
\end{this}

\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
Bartman
Posts: 366
Joined: Fri Jan 03, 2020 2:39 pm

Passing rgb colour syntax

Post by Bartman »

You pass the name of a color model as the name of a color. Please read the description of the \color command.

One way to achieve what you want would be

Code: Select all

\documentclass[a4paper,12pt]{article}
\usepackage{xcolor}

\ExplSyntaxOn
\NewDocumentEnvironment{that}{O{orange}ommm}
  { 
    #3 #4 #5
    \IfValueTF{#2}% xparse on texdoc.org
      {\color[#1]{#2}}
      {\color{#1}}
  }
  {}

\NewDocumentEnvironment{this}{O{orange}omm}
  {
    \begin{that}[#1][#2]{Something}{#3}[#4]
  }
  {
    \end{that}
  }
\ExplSyntaxOff

\begin{document}
\begin{this}{Name}{Surname}
  Some Text.
\end{this}

\begin{this}[red]{Name}{Surname}
  Some Text.
\end{this}

\begin{this}[RGB][249,104,21]{Name}{Surname}
  Some Text.
\end{this}
\end{document}
I'd find it clearer and easier to name a color using the \definecolor command in the preamble.
Post Reply