13

My goal is to create a list within a varwidth, and alternate the colors of each item. At the time, the best I could do is this:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage[
  a4paper,
  margin=15mm,
  bindingoffset=2mm,
  heightrounded,
]{geometry}

\usepackage{amsmath}
\usepackage{microtype}
\usepackage{xcolor}
\usepackage{varwidth}
\usepackage{enumitem}
\usepackage{hyperref}
\usepackage{enumitem}

\newif\ifodd
\oddtrue
\newcommand\odd{\oddtrue\item}
\newcommand\pair{\oddfalse\item}

\definecolor{lightgray}{HTML}{EFEFEF}

\begin{document}


\begingroup\setlength{\fboxsep}{6pt}
\colorbox{white}{%
  \begin{varwidth}[t]{\textwidth}
  TRIGONOMETRIA
  \begin{itemize}[leftmargin=*,label={\ifodd\color{black}\else\color{blue}\fi\textbullet}]

  \odd $\sin^{2} \alpha + \cos^{2} \alpha = 1$

  \pair \textcolor{blue}{$\tan \alpha = \dfrac{\sin \alpha}{\cos \alpha}$}

  \odd $\sin \alpha + \beta = \sin \alpha \cos \beta + \sin \beta \cos \alpha$

  \pair \textcolor{blue}{$\cos \alpha + \beta = \cos \alpha \cos \beta - \sin \alpha \sin \beta$}

  \odd $\tan \alpha + \beta = \dfrac{\tan \alpha + \tan \beta}{1 - \tan \alpha \tan \beta}$

  \pair \textcolor{blue}{$\sin \alpha - \beta = \sin \alpha \cos \beta - \sin \beta \cos \alpha$}

  \odd $\cos \alpha - \beta = \cos \alpha \cos \beta + \sin \alpha \sin \beta$

  \pair \textcolor{blue}{$\tan \alpha - \beta = \dfrac{\tan \alpha - \tan \beta}{1 + \tan \alpha \tan \beta}$}

  \odd $\sec \alpha = \dfrac{1}{\cos \alpha}$

  \pair \textcolor{blue}{$\dfrac{\sin^{2} \alpha}{\cos^{2} \alpha} + 1 = \tan^{2} \alpha + 1 = \sec^{2} \alpha$}

  \odd $\sin(\arctan z) = \cfrac{z}{\sqrt{z^{2} + 1}}$

  \pair \textcolor{blue}{$\cos(\arctan z) = \cfrac{1}{\sqrt{z^{2} + 1}}$}

  \odd $\cos 2\alpha =\begin{cases}
               \cos^{2} \alpha - \sin^{2} \alpha\\
               2 \cos^{2} \alpha - 1\\
               1 - \sin^{2} \alpha
            \end{cases}
   $

    \odd $\cos 2\alpha =\begin{cases}
               \cos^{2} \alpha - \sin^{2} \alpha\\
               2 \cos^{2} \alpha - 1\\
               1 - \sin^{2} \alpha
            \end{cases}
   $

  \end{itemize}
  \end{varwidth}%
}\endgroup

\end{document}

I would know if there is a way to define a command so I should not re-write each time

 \pair \textcolor{blue}{$...$}

Thanks in advance

11

Here is a version where the item command retains its name and its optional argument.

Sample output

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{amsmath}
\usepackage{microtype}
\usepackage{xcolor}
\usepackage{varwidth}
\usepackage{etoolbox}
\usepackage{enumitem}
\usepackage{hyperref}

\definecolor{lightgray}{HTML}{EFEFEF}

\newtoggle{odditem}
\def\myitem{\iftoggle{odditem}%
{\color{black}\togglefalse{odditem}}%
{\color{blue}\toggletrue{odditem}}%
\olditem}

\newenvironment{myitemize}[1]{%
  \begin{varwidth}[t]{\textwidth}
  #1
  \begin{itemize}[leftmargin=*]%
  \toggletrue{odditem}%
  \let\olditem\item%
  \let\item\myitem}{\end{itemize}\end{varwidth}}

\begin{document}

\begin{myitemize}{TRIGONOMETRIA}
  \item $\sin^{2} \alpha + \cos^{2} \alpha = 1$
  \item $\tan \alpha = \dfrac{\sin \alpha}{\cos \alpha}$
  \item $\sin \alpha + \beta = \sin \alpha \cos \beta + \sin \beta \cos \alpha$
  \item[$*$] $\cos \alpha + \beta = \cos \alpha \cos \beta - \sin \alpha
    \sin \beta$
  \item $\tan \alpha + \beta = \dfrac{\tan \alpha + \tan \beta}{1 -
    \tan \alpha \tan \beta}$
\end{myitemize}

\end{document}
11

You could simply use the builtin \ifodd and check the value of the item. A better solution is to define your own list type with

\newlist{altcolorslist}

and simply use

\begin{altcolorslist}
    \item ...
\begin{altcolorslist}

similar to Andrew Swann's answer.

enter image description here

References:

Code:

\documentclass{article}

\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{varwidth}
\usepackage{enumitem}

\newcommand*{\Item}{\item\ifodd\value{enumi}\color{black}\else\color{blue}\fi}

\begin{document}

\begin{varwidth}[t]{\textwidth}
  TRIGONOMETRIA
  \begin{enumerate}[leftmargin=*,label={\textcolor{black}{\textbullet}}]

  \Item  $\sin^{2} \alpha + \cos^{2} \alpha = 1$

  \Item {$\tan \alpha = \dfrac{\sin \alpha}{\cos \alpha}$}

  \Item $\sin \alpha + \beta = \sin \alpha \cos \beta + \sin \beta \cos \alpha$

  \Item {$\cos \alpha + \beta = \cos \alpha \cos \beta - \sin \alpha \sin \beta$}

  \Item $\tan \alpha + \beta = \dfrac{\tan \alpha + \tan \beta}{1 - \tan \alpha \tan \beta}$

  \Item {$\sin \alpha - \beta = \sin \alpha \cos \beta - \sin \beta \cos \alpha$}

  \Item $\cos \alpha - \beta = \cos \alpha \cos \beta + \sin \alpha \sin \beta$

  \Item {$\tan \alpha - \beta = \dfrac{\tan \alpha - \tan \beta}{1 + \tan \alpha \tan \beta}$}

  \Item $\sec \alpha = \dfrac{1}{\cos \alpha}$

  \Item {$\dfrac{\sin^{2} \alpha}{\cos^{2} \alpha} + 1 = \tan^{2} \alpha + 1 = \sec^{2} \alpha$}

  \Item $\sin(\arctan z) = \cfrac{z}{\sqrt{z^{2} + 1}}$

  \Item {$\cos(\arctan z) = \cfrac{1}{\sqrt{z^{2} + 1}}$}

  \Item $\cos 2\alpha =\begin{cases}
               \cos^{2} \alpha - \sin^{2} \alpha\\
               2 \cos^{2} \alpha - 1\\
               1 - \sin^{2} \alpha
            \end{cases}
   $

    \Item $\cos 2\alpha =\begin{cases}
               \cos^{2} \alpha - \sin^{2} \alpha\\
               2 \cos^{2} \alpha - 1\\
               1 - \sin^{2} \alpha
            \end{cases}
   $

  \end{enumerate}
\end{varwidth}%
\end{document}

Code: Custom List Version

\documentclass{article}

\usepackage{letltxmacro}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{varwidth}
\usepackage{enumitem}

\LetLtxMacro{\OldItem}{\item}
\newcommand*{\MyItem}{\OldItem\ifodd\value{altcolorslisti}\color{black}\else\color{blue}\fi}
\newlist{altcolorslist}{enumerate}{1}
\setlist[altcolorslist]{
    leftmargin=*,label={\textcolor{black}{\textbullet}},
    before={\let\item\MyItem},
    after={\let\item\OldItem},
}

\begin{document}

\begin{varwidth}[t]{\textwidth}
  TRIGONOMETRIA
  \begin{altcolorslist}

  \item $\sin^{2} \alpha + \cos^{2} \alpha = 1$

  \item $\tan \alpha = \dfrac{\sin \alpha}{\cos \alpha}$

  \item $\sin \alpha + \beta = \sin \alpha \cos \beta + \sin \beta \cos \alpha$

  \item $\cos \alpha + \beta = \cos \alpha \cos \beta - \sin \alpha \sin \beta$

  \end{altcolorslist}
\end{varwidth}%
\end{document}
  • So I can not avoid writing \textcolor{blue}{my formula} to print in blue my formula? – Salvo Matteini Apr 7 '17 at 8:34
  • @SalvoMatteini: Yes, you can now do that. Sorry that I did not notice the \textcolor{blue}.. – Peter Grill Apr 7 '17 at 8:56
10

In my opinion, a tabular is handier.

Please, note that \ifodd is a TeX primitive: you're trying (quite successfully) to break LaTeX by doing \newif\ifodd.

Linguistic note. The English “pair” means “coppia”; the English term for “pari” is “even”.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage[
  a4paper,
  margin=15mm,
  bindingoffset=2mm,
  heightrounded,
]{geometry}

\usepackage{amsmath}
\usepackage{microtype}
\usepackage[table]{xcolor}
\usepackage{enumitem}
\usepackage{array}
\usepackage{booktabs}
\usepackage{hyperref}

\newenvironment{formulas}
  {\setcounter{formulas}{0}%
   \begin{tabular}{@{}>{\changecolor\textbullet\ }l@{}}}
  {\end{tabular}}
\newcounter{formulas}
\newcommand{\changecolor}{%
  \stepcounter{formulas}%
  \ifodd\value{formulas}\color{black}\else\color{blue!70!green}\fi
}

\begin{document}

\begin{formulas}
\toprule
\multicolumn{1}{@{}l@{}}{TRIGONOMETRIA} \\
\midrule
$\sin^{2} \alpha + \cos^{2} \alpha = 1$
\\ \addlinespace
$\tan \alpha = \dfrac{\sin \alpha}{\cos \alpha}$
\\ \addlinespace
$\sin \alpha + \beta = \sin \alpha \cos \beta + \sin \beta \cos \alpha$
\\ \addlinespace
$\cos \alpha + \beta = \cos \alpha \cos \beta - \sin \alpha \sin \beta$
\\ \addlinespace
$\tan \alpha + \beta = \dfrac{\tan \alpha + \tan \beta}{1 - \tan \alpha \tan \beta}$
\\ \addlinespace
$\sin \alpha - \beta = \sin \alpha \cos \beta - \sin \beta \cos \alpha$
\\ \addlinespace
$\cos \alpha - \beta = \cos \alpha \cos \beta + \sin \alpha \sin \beta$
\\ \addlinespace
$\tan \alpha - \beta = \dfrac{\tan \alpha - \tan \beta}{1 + \tan \alpha \tan \beta}$
\\ \addlinespace
$\sec \alpha = \dfrac{1}{\cos \alpha}$
\\ \addlinespace
$\dfrac{\sin^{2} \alpha}{\cos^{2} \alpha} + 1 = \tan^{2} \alpha + 1 = \sec^{2} \alpha$
\\ \addlinespace
$\sin(\arctan z) = \cfrac{z}{\sqrt{z^{2} + 1}}$
\\ \addlinespace
$\cos(\arctan z) = \cfrac{1}{\sqrt{z^{2} + 1}}$
\\ \addlinespace
$\cos 2\alpha =\begin{cases}
               \cos^{2} \alpha - \sin^{2} \alpha\\
               2 \cos^{2} \alpha - 1\\
               1 - \sin^{2} \alpha
            \end{cases}$
\\ \addlinespace
$\cos 2\alpha =\begin{cases}
               \cos^{2} \alpha - \sin^{2} \alpha\\
               2 \cos^{2} \alpha - 1\\
               1 - \sin^{2} \alpha
            \end{cases}$
\\
\bottomrule
\end{formulas}

\end{document}

enter image description here

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.