2

I need to create a custom environment for a list which takes a parameter as item label and colors all the item labels red. Additionally, I need to be able to specify a different label for some items using the optional parameter of the item command. This different label should also be red.

So far I have the following code which works fine but I can't get the color of the ### label to red.

\newenvironment{textitemize}[1]{ 
  \renewcommand*{\labelitemi}{\color{red}#1} 
  \begin{itemize}
}{
  \end{itemize}
}

\begin{textitemize}{ad}
  \item Foo
  \item Bar
  \item[\#\#\#] foo
\end{textitemize}

EDIT

In the end it should like on the following picture, where the first line is a usage example. I would need a local counter and the items should be created by using only the \item command no other custom command.

It should look like this

3 Answers 3

1

Here is an alternative using the enumitem package:

\documentclass{article}
\usepackage{xcolor}
\usepackage{enumitem}
\newlist{myitemize}{itemize}{1}
\setlist[myitemize]{font=\color{red}}

\begin{document}
\begin{myitemize}[label=ad]
  \item Foo
  \item Bar
  \item[\#\#\#] foo
\end{myitemize}
\end{document}

Here is the updated version of the code that also inlcudes the counter:

enter image description here

\documentclass{article}
\usepackage{xcolor}
\usepackage{enumitem}
\newlist{myitemize}{enumerate}{1}
\setlist[myitemize]{font=\color{red}\bfseries\itshape}

\begin{document}
\begin{myitemize}[label=ad~\arabic*]
  \item Foo
  \item Bar
  \item[\#\#\#] foo
  \item more text
  \item another item
\end{myitemize}
\end{document}
4
  • This works fine, but I didn't include that I need to make the label bold and add a custom counter which is only incremented every non-different label. I don't see how I can achieve this with enumitem... Jun 11, 2020 at 8:16
  • To also make the labels bold, you could use font=\color{red}\bfseries. Regarding teh counter: I currently don't really understand how tht kind of counter should look like? Could you add more information on the desired output?
    – leandriis
    Jun 11, 2020 at 8:46
  • @cranckstorm: I have added an update to my answer to also include the counter.
    – leandriis
    Jun 11, 2020 at 11:44
  • Your answer is shorter and cleaner than mine therefore I have accepted it as correct - thank you. Jun 11, 2020 at 12:35
2

Based on @lendriis's answer, I add a counter to count special item with \myitem. I don't know if you want a global counter or a local one. So I define \globalmyitem and \localmyitem to switch between global and local. And please add the extra requirement you mentioned in @lendriis's answer's comment to you question.

\documentclass{article}
\usepackage{paracol}
\usepackage{xpatch}
\usepackage{xcolor}
\usepackage{enumitem}
\newlist{myitemize}{itemize}{1}
\setlist[myitemize]{font=\bfseries\itshape\color{red}, label=ad \themyitem}
\newcounter{myitem}
\setcounter{myitem}{1}
\newcommand{\myitem}{\item \stepcounter{myitem}}
\makeatletter
\newcommand{\globalmyitem}{\setcounter{myitem}{1}\def\patch@myitem{}}
\newcommand{\localmyitem}{\def\patch@myitem{\setcounter{myitem}{1}}}
\def\patch@myitem{}
\xpretocmd{\myitemize}{\patch@myitem}{}{}
\makeatother

\begin{document}
\begin{paracol}{2}
\section*{Global counter}

\globalmyitem
\fbox{list 1}
\begin{myitemize}
  \myitem Foo
  \myitem Bar
  \item[\#\#\#] foo
\end{myitemize}
\fbox{list 2}
\begin{myitemize}
  \myitem Foo
  \myitem Bar
  \item[\#\#\#] foo
\end{myitemize}

\switchcolumn
\section*{Local counter}

\localmyitem
\fbox{list 1}
\begin{myitemize}
  \myitem Foo
  \myitem Bar
  \item[\#\#\#] foo
\end{myitemize}
\fbox{list 2}
\begin{myitemize}
  \myitem Foo
  \myitem Bar
  \item[\#\#\#] foo
\end{myitemize}
\end{paracol}
\end{document}

enter image description here

0

I solved it by renewing the \item command and, depending whether the optional parameter was supplied or not, either reusing the old \item or writing my own prefix. See the code below.

Packages

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{color}
\usepackage{enumitem}
\usepackage{xifthen}% provides \isempty test

Definition

% used for counting list entries
\newcounter{myCounter}

% myPrefix will return the prefix for list items set by textitemize
\newcommand{\myPrefix}{}

% set myPrefix and start itimize environment
\newenvironment{textitemize}[1] {
    \renewcommand{\myPrefix}[1]{#1 }
    \begin{itemize}
}{\end{itemize}}

% redefine item as olditem because this will be reused
\let\olditem\item
% if optional parameter is present call \olditem with it, otherwise add custom prefix and counter
\renewcommand{\item}[1][ ]{
    \ifthenelse{\isempty{#1}}%
        {\olditem[\color{red}\textit{\textbf{\myPrefix~\stepcounter{myCounter}\themyCounter}}]}% if #1 is empty
        {\olditem[\color{red}{\textbf{#1}}]}% if #1 is not empty
    
}

Usage:

\begin{textitemize}{ad}
    \item Was wir wollen.
    \item Was wir haben.
    \item Was wir tun.
    \item[\#\#\#] Dieser Eintrag tanzt aus der Reihe.
    \item
    \item
\end{textitemize}

Result Result

You must log in to answer this question.

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