2

I am trying to define an alternative \item command that would allow me to use a different color for some of the items. My code has the following form:

\documentclass{article}
\usepackage{color}

\definecolor{gray}{rgb}{0.7,0.7,0.7}
\newcommand{\grayitem}[1]{{\color{gray} \item #1}}

\begin{document}

\begin{enumerate}
\item First item.
\grayitem Second item.
\item Third item.
\end{enumerate}

\end{document}

I was hoping that

\grayitem Second item.

would produce the same result as

{\color{gray} \item Second item.}

but for some reason, these are not equivalent. They produce the following results, respectively:

       enter image description here            enter image description here

Why are these two lines of code not equivalent? What am I doing wrong and how can I change the definition of \grayitem to produce the second result?

I have also tried removing a pair of braces, like this

\newcommand{\grayitem}[1]{\color{gray} \item #1}

but then the third item becomes gray as well.

The solution should also work for multi-paragraph items.

3

A bit hacky, but without braces:

\documentclass{article}
\usepackage{color}

\definecolor{gray}{rgb}{0.7,0.7,0.7}


\let\olditem\item
\newcommand{\grayitem}{\color{gray}\olditem}
\renewcommand{\item}{\color{black}\olditem}

\begin{document}

\begin{enumerate}
\item First item.
\grayitem Second item.
\item Third item.
\end{enumerate}

\end{document}

enter image description here


To make subliste adhere to the gray colour one could do something like the following, however if you want to use gray items inside a second level list, changing back to black won't work

\documentclass{article}
\usepackage{color}

\definecolor{gray}{rgb}{0.7,0.7,0.7}

\makeatletter
\let\olditem\item
\newcommand{\grayitem}{\color{gray}\olditem}
\renewcommand{\item}{%
    \ifnum\@enumdepth<2
        \color{black}%
    \fi%
\olditem}
\makeatother

\begin{document}

\begin{enumerate}
    \item First item.
    \grayitem Second item.
        \begin{enumerate}
            \item bla
            \item bla
        \end{enumerate}
    \item Third item.
        \begin{enumerate}
            \item bla
            \grayitem bla
            \item bla
        \end{enumerate}
\end{enumerate}

\end{document}

enter image description here

  • 1
    +1 Nice approach, but bad for those with many text colors ;) – TeXnician Mar 20 '17 at 18:24
  • 1
    @TeXnician Too many text colours are already damned to go to the typographical hell, no hope in saving them :) – user36296 Mar 20 '17 at 18:26
  • 1
    Totally right. Hence, I've also added a hacky two-color approach (probably with very bad style). – TeXnician Mar 20 '17 at 18:46
  • @TeXnician Nice hack :) – user36296 Mar 20 '17 at 19:52
  • +1. Much appreciated. (Sadly, I cannot accept both answers.) @TeXnician: Interestingly, both hacks seem to stop working when the \grayitem in question contains another list. But I did ask for "everything between one \item and the next" in my comment =) – Dejan Govc Mar 21 '17 at 23:38
2

The solution is simple: You forgot braces around your argument. You would have to use \grayitem{Second item.}. Else TeX uses only the first token (the "S") and the rest is not in #1 anymore.

In fact that would be your document with right color:

\documentclass{article}
\usepackage{color}

\definecolor{gray}{rgb}{0.7,0.7,0.7}
\newcommand{\grayitem}[1]{{\color{gray}\item #1}}

\begin{document}

\begin{enumerate}
\item First item.
\grayitem{Second item.}
\item Third item.
\end{enumerate}

\end{document}

Update: As I was challenged (;)) here's another solution for items without braces using etoolbox in a slightly unintended manner:

\documentclass{article}
\usepackage{color}
\usepackage{etoolbox}

\definecolor{gray}{rgb}{0.7,0.7,0.7}
\let\olditem\item
\newcommand{\grayitem}{\let\item\olditem\color{gray}\item\preto\item{\color{black}}}

\begin{document}

\begin{enumerate}
\item First item.
\grayitem Second item.
\item Third item.
\grayitem Another one.
\item Test
\end{enumerate}

\end{document}
  • Thanks! You're right, this works. Is there a way of doing it without adding the braces? I'm asking because \item does not seem to require these braces and it would make it easier to switch between the two. Can #1 be somehow forced to consist of everything between one \item and the next? – Dejan Govc Mar 19 '17 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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