1

I want to have the ability to colour the text using an optional argument (defaulted to orange). Although Something Something should not be coloured.

\documentclass{article}
\usepackage{xcolor}

\NewDocumentEnvironment{Statement}{O{}}
  {
    Something Something
  }
  { \vspace{0.3cm} }

\begin{document}

\begin{Statement}
  This text is displayed in color.
\end{Statement}

\end{document}
4
  • 2
    \NewDocumentEnvironment{Statement}{O{orange}} {\color{#1} Commented Oct 10, 2023 at 16:35
  • I would need to reset the colour to the original after the environment ends.
    – Veak
    Commented Oct 10, 2023 at 16:43
  • 2
    No you would not, environments are groups, everything resets. Commented Oct 10, 2023 at 16:45
  • All is very well then.
    – Veak
    Commented Oct 10, 2023 at 16:46

2 Answers 2

1

You just need to specify Orange as the default in O{}

enter image description here

\usepackage{xcolor}

\NewDocumentEnvironment{Statement}{O{orange}}
  {%
    Something Something \color{#1}%
  }
  { \vspace{0.3cm} }

\begin{document}

\begin{Statement}
  This text is displayed in color.
\end{Statement}

Black
\end{document}
0

As said by David Carlisle, an environment is a group. \color{} won't affect the text after \end{statement} closing marker.

If you want a default color in orange, you only have to declare it in the definition and use \color{#1} to apply the chosen color (if not, it will be orange).

See here :

\documentclass{article}
\usepackage{xcolor}

\newenvironment{statement}[1][orange]%
{%
    \color{#1}% other stuff to apply at the start of the environment
}{%
    %%%Stuff here to apply at the end
}
\begin{document}
    This is normal text.
    \begin{statement}
        This text is displayed in color. Default is orange.
    \end{statement}
    
    This is normal text.
    
    \begin{statement}[purple]
        This text is displayed in purple.
    \end{statement}
    
    This is normal text
\end{document}

enter image description here

1
  • I don't think this answers the question as the OP wanted some black text in the begin text, which you've removed. Commented Oct 10, 2023 at 17:02

You must log in to answer this question.

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