TeX \newcommand 和 DRY(不要重复自己)

TeX \newcommand 和 DRY(不要重复自己)

我的目标是创建一个宽度可变的列表,并交替显示每个项目的颜色。当时,我能做的最好的事情是:

\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}

我想知道是否有办法定义命令,这样我就不必每次都重写

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

提前致谢

答案1

这是 item 命令保留其名称和可选参数的版本。

示例输出

\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}

答案2

您可以简单地使用内置函数\ifodd并检查项目的值。更好的解决方案是使用以下代码定义您自己的列表类型:

\newlist{altcolorslist}

并简单地使用

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

如同安德鲁·斯旺的回答。

在此处输入图片描述

参考:

代码:

\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}

代码:自定义列表版本

\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}

答案3

我认为,atabular更方便。

请注意,这\ifodd是一个 TeX 原语:您正在尝试(相当成功)通过执行以下操作来破坏 LaTeX \newif\ifodd

语言注释。英文中的“pair”意为“coppia”;英文中的“pari”意为“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}

在此处输入图片描述

相关内容