6

I defined a command

\newcommand{\done}{\color[RGB]{124, 124, 255}}

Is there a way to use it as

\item \done la-la

instead of

\item {\done la-la}

It bothers me to put brackets all the time.
So basically I want \done to have a scope only within \item.

3 Answers 3

5

Reset the color with \item

\documentclass{article}
\usepackage{xcolor}
\newcommand\done{\color[RGB]{124, 124, 255}}
\let\Item\item
\renewcommand\item{\normalcolor\Item}

\begin{document}

\begin{enumerate}
\item \done la-la
\item foo
\item \done la-la
\end{enumerate}

\end{document}
6
  • \expandafter\renewcommand\expandafter\item\expandafter{\expandafter\normalcolor\item} would be better than going through \let\Item\item. Mar 29, 2011 at 8:03
  • 1
    @Bruno why? I always use \let to avoid these incomprehensible \expandafter worms. Are there any drawbacks (apart from the fact that this doesn’t always work)? Mar 29, 2011 at 11:54
  • @Bruno @Konrad it seems to me the trade-off is the use of a one-off macro for a lot more readability. Does it make a difference if the \renewcommand\item is within the start-code of Lenumerate? Mar 29, 2011 at 12:17
  • 4
    Also the \pretocmd macro from the etoolbox could be used to easily add the \normalcolor to \item. Mar 29, 2011 at 13:16
  • @Konrad, I guess that most often this is right. It just seems like polluting the namespace, and risking more name clashes. But yes, it is mostly irrelevant. With \pretocmd that @Martin mentions, things are both simple and clean. Mar 29, 2011 at 16:21
3

Bulding on Gonzalo's solution, you can alter the definition of \item within the Lenumerate environment and behavior outside it will be as normal.

\documentclass{article}

\newenvironment{Lenumerate}{%
  \begingroup
  \let\latexsaveditem\item
  \renewcommand{\item}{\endgroup\begingroup\latexsaveditem}
  \enumerate\begingroup%
}{%
  \endgroup\endenumerate\endgroup%
}

\begin{document}

\begin{Lenumerate}
  \item \bfseries a
  \item \itshape b
  \item \scshape c
  \item d
\end{Lenumerate}

\end{document}
2

This answer is to be considered somehow experimental; I defined three commands: \Mfitem (to be used exclusively in the first item of the list), \Mitem (to be used exclusively for items different from the first and the last one) and \Mlitem (to be used exclusively for the last item of the list). \Mfitem simply invokes \item and begins a group (without closing it); \Mitem ends a group, opens another one, and then invokes \item (thanks to Caramdir for suggesting this order), and \Mlitem closes the last group before invoking \item. An example:

\documentclass{article}

\newcommand\Mfitem{\begingroup\item}
\newcommand\Mitem{\endgroup\begingroup\item}
\newcommand\Mlitem{\endgroup\item}

\begin{document}

\begin{enumerate}
  \Mfitem \bfseries a
  \Mitem \itshape b
  \Mitem \scshape c
  \Mlitem d
\end{enumerate}

\end{document}

EDIT: Following a suggestion by Caramdir, here's another option: to define a new environment using environment but adding \begingroup at the beginning and \endgroup at the end; now the commands \Mfitem and \Mlitem are no longer necessary:

\documentclass{article}

\newenvironment{Lenumerate}
  {\enumerate\begingroup}
  {\endgroup\endenumerate}

\newcommand\Mitem{\endgroup\begingroup\item}

\begin{document}

\begin{Lenumerate}
  \Mitem \bfseries a
  \Mitem \itshape b
  \Mitem \scshape c
  \Mitem d
\end{Lenumerate}

\end{document}
4
  • 1
    If you swap \item and \begingroup in \Mitem, the optional argument will work. Also, if you define an environment based on enumerate with \begingroup at the beginning and \endgroup at the end, you will not need \Mfitem and \Mlitem.
    – Caramdir
    Mar 29, 2011 at 3:12
  • @Caramdir: following your suggestion, I tried something like \newenvironment{Lenumerate} {\enumerate\begingroup} {\endgroup\endenumerate} \newcommand\Mitem[1][]{\endgroup\begingroup\item[#1]}but then all the \Mitem commands won't produce numbering. Maybe I misunderstood your idea? Mar 29, 2011 at 3:27
  • just \newcommand\Mitem{\endgroup\begingroup\item} will pick up any optional arguments.
    – Caramdir
    Mar 29, 2011 at 3:34
  • @Caramdir: yes, thank you. I'll edit my answer to include your suggestion. Mar 29, 2011 at 3:37

You must log in to answer this question.

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