tex中ifthen宏包的介绍
文章目录
- 介绍
- 定义bool变量
- 布尔运算
- ifthenelse语法
- whiledo语法
介绍
ifthen 宏包提供了简单、直观的条件判断功能,适合在文档中按条件输出不同内容。核心思想是先定义布尔变量或直接写条件表达式,然后用\newboolean、\setboolean 定义和赋值布尔变量\ifthenelse{<条件>}{<真时输出>}{<假时输出>}
来进行分支输出。同时,它还支持简单的逻辑组合,如 \AND、\OR、\NOT(或通过 \boolean{…} 结合布尔变量使用)。
定义bool变量
\newboolean{A}
\newboolean{B}
\setboolean{A}{true}
\setboolean{B}{false}
布尔运算
- 比较大小:>, <, =
- 判断字符串是否相等:\equal{⟨string⟩}{⟨string⟩}
- 逻辑组合:\AND、\OR、\NOT
- 字符串是否为空:\isempty{}
- 判断奇偶性:\isodd{ ⟨number⟩ }
- true if \cmd is not defined:\isundefined{ \cmd }
- 长度比较:
\lengthtest{⟨dimen⟩<⟨dimen⟩}
\lengthtest{⟨dimen⟩=⟨dimen⟩}
\lengthtest{⟨dimen⟩>⟨dimen⟩}
ifthenelse语法
\ifthenelse{<条件>}{<真时输出>}{<假时输出>}
例如:
\documentclass{article}
\usepackage{datatool}
\usepackage{xcolor}
\usepackage{ifthen}
\newcommand{\red}{\color{red}}
\newcommand{\blue}{\color{blue}}
\begin{document}\DTLloaddb{mydata}{data.csv}\begin{itemize}
\DTLforeach{mydata}{\year=year, \title=title, \author=author}{\ifthenelse{ \year>2013 \and \year<2025}{% 循环体\item The year is {\year}, the title is {\title}, author is {\author}.
}
{%条件为假什么也不做}
}
\end{itemize}
whiledo语法
\whiledo{⟨test⟩}{⟨while clause⟩}
