3

Consider the following simple code:

\documentclass[11pt]{article} 
\begin{document}        
\begin{itemize}
    \item plain text
    \item I want this to be blue
    \item another text
\end{itemize}
\end{document}

The result of the code is

enter image description here

Now, suppose that I want to make the text color of the second item blue. One can manually use the textcolor command, but the thing is that in my real example there are many places to change the text, and such places are frequently modified. Hence, it is desirable to write a macro to change the color simply.

Question: How can I define custom command like \blueitem, such that replacing \item to \blueitem in the above code gives the desired result? Or, as an alternative, one can also think of a macro that using \item[color=blue] instead of \item gives the result.

Edit: The answer by Sandy G does not work when used with the proof environment. For example:

\documentclass[11pt]{article}
\usepackage{amsthm}
\usepackage{xcolor}
\let\olditem\item\renewcommand{\item}[1][black]{\color{#1}\olditem} 
\begin{document}
\begin{itemize}
    \item[blue] abc
    \begin{proof}
        abc.
    \end{proof}
\end{itemize}
\end{document}

This code gives an error message

Package xcolor Error: Undefined color `\hskip \labelsep \itshape Proof\relax '. a

and the result is:

enter image description here

Note that there is no "Proof." symbol.

2 Answers 2

7

You can redefine \item to accept an optional argument for the color using xcolor.

enter image description here

\documentclass[11pt]{article} 
\usepackage{xcolor}

\let\olditem\item\renewcommand{\item}[1][black]{\color{#1}\olditem}

\begin{document}        
\begin{itemize}
    \item plain text
    \item[blue] I want this to be blue
    \item[red] another text
    \item one more
\end{itemize}
\end{document}

The main problem with this approach is that some environments use lists, and there are undesired consequences. Notably, a proof from amsthm is a list, so this won't work. A better solution is to make a new command, for example \citem that accepts an optional color argument:

\newcommand{\citem}[1][black]{\color{#1}\item}

enter image description here

One downside to this approach is that \citem will change the color of all \items below it in the list. So use \citem with no color (black is the default) to change back to black for the rest of the list. Then you can use \item and \citem in the same list.

\documentclass[11pt]{article}
\usepackage{amsthm}
\usepackage{xcolor}
\newcommand{\citem}[1][black]{\color{#1}\item} 
\begin{document}

\begin{itemize}
  \citem[blue] abc
    \begin{proof}
        abc.
    \end{proof}
  \citem[red] abc
    \begin{proof}
        abc.
    \end{proof}
    
\end{itemize}
\end{document}
7
  • This is exactly what I want. Thanks for your answer!
    – eigenvalue
    Sep 1, 2022 at 4:02
  • Sadly, when \item is used together with the proof environment, there seems a problem (please see my edited post). How can I fix this problem?
    – eigenvalue
    Sep 1, 2022 at 4:12
  • My suggestion/method works with proof environment also. Only thing as noted is to put the stuff between braces.
    – mas
    Sep 1, 2022 at 12:15
  • 1
    @eigenvalue: I updated my solution so it works with proof.
    – Sandy G
    Sep 1, 2022 at 12:47
  • +1 but may I suggest something along the lines of \renewcommand{\item}[1][black]{\if\relax\detokenize{#1}\relax\color{black}\else\color{#1}\fi\olditem}? In this way you can use the "second" optional argument by leaving the first one empty.
    – campa
    Sep 1, 2022 at 13:02
2

This is one way to do it:

\documentclass[11pt]{article}                                                     
\usepackage{xcolor}                                                               
                                                                                  
\newcommand\colitem[2]{%                                                          
  {\color{#1}\item {#2}}                                                          
}                                                                                 
                                                                                  
\newcommand\reditem[1]{%                                                          
  \colitem{red}{#1}                                                               
}                                                                                 
                                                                                  
\newcommand\blueitem[1]{%                                                         
  \colitem{blue}{#1}                                                              
}                                                                                 
                                                                                  
\begin{document}                                                                  
\begin{itemize}                                                                   
    \reditem {plain text}                                                         
    \blueitem {I want this to be blue}                                            
    \item another text                                                            
\end{itemize}                                                                     
\end{document}

The output will be like this:

enter image description here

2
  • Thanks a lot for your answer. However, one sad thing is that I need parenthesis {...} in \blueitem {I want this to be blue} to make this work. Is there way to use this command without using the parenthesis?
    – eigenvalue
    Sep 1, 2022 at 2:54
  • Yes. I could not find a way to do it. Someone more knowledgeable will help you.
    – mas
    Sep 1, 2022 at 3:48

You must log in to answer this question.

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