1

I am defining an environment for which the texts should be red. I thought the bgroup and egroup will ensures that only the box is red, but the text following the box also becomes colored.

Doesn't a group keep it local and how can I change it in this case?

\documentclass{article}
\usepackage[dvipsnames]{xcolor}

\newenvironment{comment} {\par\medskip \bgroup\color{red}% \textbf{Hello: } } { \egroup\medskip }

\begin{document} Test text begin below:

\comment{ Inside environment }

1234 (should be black)  % but it is appearing red

\end{document}

bluk
  • 301
  • you do not need bgroup and egroup as an environment forms a group, but as you defined it as an environment you need to use as \begin....\end` Also never ignore errors, if you get any error ask about the error message not about any PDF that is generated (which is never intended to be usable after an error) – David Carlisle Aug 14 '21 at 08:19

1 Answers1

2

A group does, but you're using it incorrectly. Compiling your code reveals the following in the .log:

(\end occurred inside a group at level 1)

simple group (level 1) entered at line 17 ({)

bottom level

This shows that you opened a group but never closed it. This evident from using a command-form of the environment comment that you defined without the closing portion of it, namely \endcomment.

\comment is replaced with a group opening \bgroup, but since you never call \endcomment there is no \egroup.

Options here depends on what you want to use. The following true command-form version works:

\newcommand{\comment}[1]{%
  \par\medskip
  \bgroup\color{red}%
  \textbf{Hello: }%
  #1
  \egroup\par\medskip
}

enter image description here

Related: What happens if you use a command form of an environment?

Werner
  • 603,163