0

The following command implemented by @egreg makes a new environment that can be easily coloured. I would like to construct a number of similar environments where a different text is used instead of Typex, for the case of corollary, lemma and others.

I have the option of repeating the same code. But to avoid writing the same code several times, would there be a better way, perhaps deriving the rest from \teora?

\newcommand{\teoracolor}{}% initialize
\newtheorem{teorainner}{\color{\teoracolor}Typex}[section]
\NewDocumentEnvironment{teora}{D(){teora}}
 {\renewcommand{\teoracolor}{#1}\teorainner}
 {\endteorainner}

\colorlet{teora}{green!80!blue}

1 Answer 1

2

You define an abstraction of all the necessary parts.

I define \newcolortheorem that has the same syntax as \newtheorem, but with a trailing optional argument in parentheses that contains the color for the label (changeable at runtime in the same way as specified in the previous answer).

\documentclass{article}
\usepackage{xcolor}
\usepackage{amsthm}

\NewDocumentCommand{\newcolortheorem}{momoD(){black}}{%
  % #1 = environment name
  % #2 = shared counter
  % #3 = label
  % #4 = parent counter
  % #5 = default color for the label
  % first define the theorem-like environment
  \IfNoValueTF{#4}{% no parent counter
    \IfNoValueTF{#2}{% no shared counter
      \newtheorem{#1inner}{\color{\theoremcolor}#3}%
    }{% shared counter
      \newtheorem{#1inner}[#2]{\color{\theoremcolor}#3}%
    }%
  }{% parent counter
    \newtheorem{#1inner}{\color{\theoremcolor}#3}[#4]%
  }%
  % define the outer environment
  \NewDocumentEnvironment{#1}{D(){#5}}{%
    \renewcommand{\theoremcolor}{##1}\UseName{#1inner}%
  }{%
    \UseName{end#1inner}%
  }%
}
\newcommand{\theoremcolor}{}% initialize

\colorlet{teora}{green!80!blue}

\newcolortheorem{teora}{Theorem}[section](teora)

\newcolortheorem{lemma}{Lemma}

\begin{document}

\section{Title}

\begin{lemma}
Standard lemma
\end{lemma}

\begin{lemma}(blue!50)
A blue lemma
\end{lemma}

\begin{teora}
This theorem is green, with no name.
\end{teora}

\begin{teora}[Name]
This theorem is green, with a name.
\end{teora}

\begin{teora}(red!80)
This theorem is red, with no name.
\end{teora}

\begin{teora}(red!80)[Name]
This theorem is red, with a name.
\end{teora}

\end{document}

enter image description here

A version with a key-value system.

\documentclass{article}
\usepackage{xcolor}
\usepackage{amsthm}

\ExplSyntaxOn

\keys_define:nn { konmi/theorem }
 {
  name .tl_set:N = \l__konmi_theorem_name_tl,
  label .tl_set:N = \l__konmi_theorem_label_tl,
  shared .tl_set:N = \l__konmi_theorem_shared_tl,
  parent .tl_set:N = \l__konmi_theorem_parent_tl,
  labelcolor .tl_set:N = \l__konmi_theorem_labelcolor_tl,
 }

\NewDocumentCommand{\newcolortheorem}{m}
 {
  \konmi_newtheorem:n { #1 }
 }

\tl_new:N \l__konmi_theorem_setcolor_tl
\exp_args_generate:n { Ve }

% syntactic sugar
\cs_new_protected:Nn \__konmi_newtheorem_simple:nn
 {
  \newtheorem{#1_inner}{\color{\l__konmi_theorem_setcolor_tl}#2}
 }
\cs_generate_variant:Nn \__konmi_newtheorem_simple:nn { VV }

\cs_new_protected:Nn \__konmi_newtheorem_shared:nnn
 {
  \newtheorem{#1_inner}[#3]{\color{\l__konmi_theorem_setcolor_tl}#2}
 }
\cs_generate_variant:Nn \__konmi_newtheorem_shared:nnn { VVV }

\cs_new_protected:Nn \__konmi_newtheorem_parent:nnn
 {
  \newtheorem{#1_inner}{\color{\l__konmi_theorem_setcolor_tl}#2}[#3]
 }
\cs_generate_variant:Nn \__konmi_newtheorem_parent:nnn { VVV }

% main function
\cs_new_protected:Nn \konmi_newtheorem:n
 {
  % evaluate the keys
  \keys_set:nn { konmi/theorem }
   {
    % first reset, then apply the input
    name =, label =, shared =, parent =, labelcolor = black, #1
   }
  % first define the theorem-like environment
  \tl_if_empty:VTF \l__konmi_theorem_parent_tl
   {% no parent counter
    \tl_if_empty:VTF \l__konmi_theorem_shared_tl
     {
      \__konmi_newtheorem_simple:VV
        \l__konmi_theorem_name_tl
        \l__konmi_theorem_label_tl
     }
     {% shared counter
      \__konmi_newtheorem_shared:VVV
        \l__konmi_theorem_name_tl
        \l__konmi_theorem_label_tl
        \l__konmi_theorem_shared_tl
     }
   }
   {% parent counter
    \__konmi_newtheorem_parent:VVV
      \l__konmi_theorem_name_tl
      \l__konmi_theorem_label_tl
      \l__konmi_theorem_parent_tl
  }%
  % define the outer environment
  \__konmi_newtheorem_define:VV \l__konmi_theorem_name_tl \l__konmi_theorem_labelcolor_tl
 }

\cs_new_protected:Nn \__konmi_newtheorem_define:nn
 {
  \NewDocumentEnvironment { #1 } {D(){#2}}
   {
    \tl_set:Nn \l__konmi_theorem_setcolor_tl { ##1 }
    \use:c { #1_inner }
   }
   {
    \use:c { end#1_inner }
   }
 }
\cs_generate_variant:Nn \__konmi_newtheorem_define:nn { VV }

\ExplSyntaxOff

\colorlet{teora}{green!80!blue}

\newcolortheorem{
  name=teora,
  label=Theorem,
  parent=section,
  labelcolor=teora,
}

\newcolortheorem{
  name=lemma,
  label=Lemma,
}

\begin{document}

\section{Title}

\begin{lemma}
Standard lemma
\end{lemma}

\begin{lemma}(blue!50)
A blue lemma
\end{lemma}

\begin{teora}
This theorem is green, with no name.
\end{teora}

\begin{teora}[Name]
This theorem is green, with a name.
\end{teora}

\begin{teora}(red!80)
This theorem is red, with no name.
\end{teora}

\begin{teora}(red!80)[Name]
This theorem is red, with a name.
\end{teora}

\end{document}
14
  • Would you be so kind to keep the various solutions separate. They are especially instructive for people to scrutinise.
    – Veak
    Oct 1, 2022 at 0:08
  • Have uncountered the following problem ! Undefined control sequence. \environment teora code ...remcolor }{#1}\UseName {teorainner} l.414 \begin{teora}(red!80) [Name]
    – Veak
    Oct 1, 2022 at 0:50
  • @konmi You don’t have an up-to-date TeX system.
    – egreg
    Oct 1, 2022 at 7:49
  • I updated Texlive and the code is now working.
    – Veak
    Oct 1, 2022 at 18:47
  • 1
    @konmi No, / is not a letter, but it can be used in that context. Can you see \newcommand\theoremcolor{}? Then you have the answer.
    – egreg
    Oct 3, 2022 at 8:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .