Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers

up vote 5 down vote accepted

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}
share|improve this answer
    
\expandafter\renewcommand\expandafter\item\expandafter{\expandafter\normalcolor‌​\item} would be better than going through \let\Item\item. –  Bruno Le Floch Mar 29 '11 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)? –  Konrad Rudolph Mar 29 '11 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? –  Matthew Leingang Mar 29 '11 at 12:17
4  
Also the \pretocmd macro from the etoolbox could be used to easily add the \normalcolor to \item. –  Martin Scharrer Mar 29 '11 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. –  Bruno Le Floch Mar 29 '11 at 16:21
show 1 more comment

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}
share|improve this answer
add comment

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}
share|improve this answer
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 '11 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? –  Gonzalo Medina Mar 29 '11 at 3:27
    
just \newcommand\Mitem{\endgroup\begingroup\item} will pick up any optional arguments. –  Caramdir Mar 29 '11 at 3:34
    
@Caramdir: yes, thank you. I'll edit my answer to include your suggestion. –  Gonzalo Medina Mar 29 '11 at 3:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.