\newboolean
? What is boolean anyway ? What or why is ifthen package useful ? And what is it used for ? What is the difference between ifthen package and xparse package?General ⇒ Using boolean Values
-
- Posts: 402
- Joined: Fri May 20, 2011 9:41 am
Using boolean Values
In the xifthen package how do you use
NEW: TikZ book now 40% off at Amazon.com for a short time.

- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
Using boolean Values
Boolean data type – Wikipedia, the free encyclopediaghostanime2001 wrote:[…] What is boolean anyway ? […]
Thorsten
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
-
- Posts: 402
- Joined: Fri May 20, 2011 9:41 am
Re: Using boolean Values
in the context of LaTeX ?
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
Using boolean Values
In which context else? You asked about Boolean variables. Actually the context is irrelevant. The principal is the same in LaTeX as it is in programming languages like e.g. C++.ghostanime2001 wrote:in the context of LaTeX ?
Reading the respective manuals should show you the differences.ghostanime2001 wrote:[…] What is the difference between ifthen package and xparse package?
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
- Stefan Kottwitz
- Site Admin
- Posts: 10347
- Joined: Mon Mar 10, 2008 9:44 pm
Using boolean Values
ifthen
is not recommendable today. For example, \ifthenelse
is fragile and cannot be used in moving arguments, such as with \caption
or \section
.Today, etex provides newer commands. Have a look at the

Stefan
LaTeX.org admin
Using boolean Values
Like the website Thorsten links to explains a boolean is a data-type that can have two different values,ghostanime2001 wrote:What is boolean anyway ?
0
and 1
, say, or more often and semantically more meaningful false
and true
.These data-types are useful for programming which means they are more useful in package writing than for composing a document but may come in handy in custom macros, too, of course. Since TeX's primitive booleans are specific ones and generic testing is not very handy LaTeX provides a macroghostanime2001 wrote:What or why is ifthen package useful ? And what is it used for ?
\newif
to create new boolean flags. The syntax, however, still is the same as TeX's. It is not easy to handle (for example test cannot easiliy be combined which can lead to rather lengthy code) and very error prone if your not familiar with concepts like expansion and the like. This is why packages like ifthen, xifthen, etoolbox or the LaTeX3-kernel provide a more flexible interface. Although they're all aiming at package or class authors they can of course be used to create own macros for a specific document.They have totally different goals. ifthen, as I tried to explain above, aims at providing an easier interface to boolean tests while xparse provides means for a more flexible command definition interface than traditional LaTeX2e'sghostanime2001 wrote:What is the difference between ifthen package and xparse package?
\newcommand
.I'll show an example not for xifthen but for etoolbox and itsghostanime2001 wrote:In the xifthen package how do you use\newboolean
?
\newbool
. Let's say you want to create a document both for on-screen viewing and for print. The on-screen version may use some colors and hyperlinks and whatnot while the printed version should use no colors or at least no colored links. You could do something along these lines:Code: Select all
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{xcolor}
\colorlet{print}{black}
\colorlet{screen}{blue}
\usepackage{etoolbox}
% new boolean flag:
\newbool{print}
% it is initially set to `false' and can be set by issuing
% \booltrue{print} or \boolfalse{print}
% its value can be tested with
% \ifbool{print}{<true branch>}{<false branch>}
\newcommand*\name[1]{%
\textsc{%
\ifbool{print}
{\textcolor{print}{#1}}
{\textcolor{screen}{#1}}%
}%
}
% uncomment to see the difference:
% \booltrue{print}
\usepackage{hyperref}
\ifbool{print}{}
{
\hypersetup{
colorlinks,
allcolors = link
}}
\begin{document}
\section{A section}\label{sec:section}
Here is a name \name{ghostanime2001} and a reference to
section~\ref{sec:section}.
\end{document}
site moderator & package author
-
- Posts: 402
- Joined: Fri May 20, 2011 9:41 am
Using boolean Values
Okay that was kind of difficult to understand. Maybe I dived in too deep. I used to program in 10th grade but does all this have anything to do with if and else statements ? For example, a default command is defined. If it exceeds some value or some definition then use this command
#1
and if it doesn't exceed or is some other definition then use command #2
? If that's the case, or could be coded in LaTeX, that would be very useful. I guess that's where I'm trying to get at but may have used something totally different ?Using boolean Values
I am no programmer in the sense that I haven't learned it and besides (La)TeX only had superficial contact with other programming languages. However, they all have these means to evaluate these statementsghostanime2001 wrote:Okay that was kind of difficult to understand. Maybe I dived in too deep. I used to program in 10th grade but does all this have anything to do with if and else statements ?
Code: Select all
IF statement
do something if statement is true
ELSE
do something if it is false
FI
Code: Select all
\ifsometest{<statement>}
{<do something if statement is true>}
{<do something if it is false>}
I'm not sure I get what you mean but LaTeX commands can be defined to do something depending on the input and quite often the input is evaluated with a boolean test at some point. If you're after a specific application you could describe exactly what you want and what input syntax you have in mind and I'm confident we can come up with something.ghostanime2001 wrote:For example, a default command is defined. If it exeeds some value or some definition then use this command #1 and if it doesn't exceed or is some other definition then use command #2 ? If that's the case, or could be coded in latex, that would be very useful. I guess that's where I'm trying to get at but may have used something totally different ?
Regards
site moderator & package author
-
- Posts: 402
- Joined: Fri May 20, 2011 9:41 am
Using boolean Values
There is a specific application for this. I want to make equilibrium tables easier to typeset so I wouldn't have to write long codes.
I want the largest height of the compounds in the first line to adjust to have that largest height in the other two compounds so that all the boxes in the first line have uniform height. In this code for example, let's say CH3COO- has the largest height, I want to make CH3COOH and H+ have the height of CH3COO- so that all (CH3COOH, CH3COO-, H+) have the same heights.
In a similar manner, for the last line, I want the width in the last line adjusted to the width in the first line of the same column. For example, CH3COOH has a wide width and the "x" in the last line of that column has a very small width. I want the width of "x" to be the same as CH3COOH. Similarly, for the CH3COO- column done in a similar manner.
Last but not least, I want the 0.48 in the third column to have the largest width in that column. For example, the largest width in that column is 0.48-x so I want 0.48 to have the exact width as 0.48-x. Also, I want the height of 0.48 to be the same has the height of "I" in the left hand side. This would straighten all the lines (horizontal and vertical lines).
I want the largest height of the compounds in the first line to adjust to have that largest height in the other two compounds so that all the boxes in the first line have uniform height. In this code for example, let's say CH3COO- has the largest height, I want to make CH3COOH and H+ have the height of CH3COO- so that all (CH3COOH, CH3COO-, H+) have the same heights.
In a similar manner, for the last line, I want the width in the last line adjusted to the width in the first line of the same column. For example, CH3COOH has a wide width and the "x" in the last line of that column has a very small width. I want the width of "x" to be the same as CH3COOH. Similarly, for the CH3COO- column done in a similar manner.
Last but not least, I want the 0.48 in the third column to have the largest width in that column. For example, the largest width in that column is 0.48-x so I want 0.48 to have the exact width as 0.48-x. Also, I want the height of 0.48 to be the same has the height of "I" in the left hand side. This would straighten all the lines (horizontal and vertical lines).
Code: Select all
\documentclass[fleqn]{article}
\usepackage[T1]{fontenc}
\usepackage{fullpage}
\usepackage{makebox}
\usepackage[version=3]{mhchem}
\usepackage{tikz}
\usetikzlibrary{matrix,calc}
\setlength{\parindent}{0pt}
\setlength{\parskip}{0pt}
\setlength{\mathindent}{0pt}
\begin{document}
\begin{tikzpicture}[nodes=draw,baseline=(m-1-1.base)]
\matrix (m) [matrix of math nodes,ampersand replacement=\&]
{
\vphantom{.} \& \node (a) {\ce{CH3COOH <=>}}; \& \node (b) {\ce{CH3COO- +}}; \& {\ce{H+}} \\
\node (c) {\ce{I}}; \& \node {0}; \& \node {1}; \& \node (d) {0.48}; \\
\node (e) {\ce{C}}; \& \node {+x}; \& \node {-x}; \& \node {-x}; \\
\ce{E} \& \node (f) {x}; \& \node (g) {1-x}; \& \node {0.48-x}; \\
};
\draw (a.north east) -- (f.south east);
\draw (a.north west) -- (f.south west);
\draw (b.north east) -- (g.south east);
\draw (c.north west) -- (d.north east);
\end{tikzpicture}
\end{document}
Using boolean Values
It seems to me you want to create some kind of tabular layout. Why don't you simply use a
(IMHO the table would be just as clear and even more readable if you omitted all the lines.)
Regards
{tabular}
? Anyway, using the (A -| B)
or (A |- B)
syntax for coordinates you don't need to measure anything here.Code: Select all
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[version=3]{mhchem}
\usepackage{tikz}
\usetikzlibrary{matrix,calc}
\usepackage{array}
\begin{document}
\begin{tikzpicture}[baseline=(m-1-1.base)]
\matrix (m) [matrix of math nodes,ampersand replacement=\&]
{
\vphantom{.} \& \node (a) {\ce{CH3COOH <=>}}; \& \node (b) {\ce{CH3COO- +}}; \& {\ce{H+}} \\
\node (c) {\ce{I}}; \& \node {0}; \& \node {1}; \& \node (d) {0.48}; \\
\node (e) {\ce{C}}; \& \node {+x}; \& \node {-x}; \& \node {-x}; \\
\ce{E} \& \node (f) {x}; \& \node (g) {1-x}; \& \node (h) {0.48-x}; \\
};
\draw (a.north east) -- (f.south east -| a.north east);
\draw (a.north west) -- (f.south west -| a.north west);
\draw (a.north east -| b.north east) -- (g.south east -| b.north east);
\draw (c.north west) -- (d.north east -| h.south east);
\end{tikzpicture}
\begin{tikzpicture}[baseline=(m-1-1.base)]
\matrix (m) [nodes={minimum height=1.5em},matrix of math nodes,ampersand replacement=\&]
{
\& \ce{CH3COOH <=>} \& \ce{CH3COO- +} \& \ce{H+} \\
\ce{I} \& 0 \& 1 \& 0.48 \\
\ce{C} \& +x \& -x \& -x \\
\ce{E} \& x \& 1-x \& 0.48-x \\
};
\draw (m-1-1.south west) -- (m-1-4.south east -| m-4-4.east);
\draw (m-1-2.north west) -- (m-4-2.south west -| m-1-2.west);
\draw (m-1-3.north west) -- (m-4-3.south west -| m-1-3.west);
\draw (m-1-2.north -| m-4-4.south west) -- (m-4-4.south west);
\end{tikzpicture}
\begin{tabular}{>{$}c<{$}*{3}{|>{$}c<{$}}}
& \ce{CH3COOH <=>} & \ce{CH3COO- +} & \ce{H+} \\\hline
\ce{I} & 0 & 1 & 0.48 \\
\ce{C} & +x & -x & -x \\
\ce{E} & x & 1-x & 0.48-x
\end{tabular}
\end{document}
Regards
site moderator & package author