LaTeX forum ⇒ GeneralUsing boolean Values

LaTeX specific issues not fitting into one of the other forums of this category.
ghostanime2001
Posts: 402
Joined: Fri May 20, 2011 9:41 am

Using boolean Values

Postby ghostanime2001 » Fri Mar 08, 2013 3:30 am

In the xifthen package how do you use \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?

Tags:

User avatar
localghost
Site Moderator
Posts: 9204
Joined: Fri Feb 02, 2007 12:06 pm

Postby localghost » Fri Mar 08, 2013 10:41 am

ghostanime2001 wrote:[…] What is boolean anyway ? […]

Boolean data type – Wikipedia, the free encyclopedia


Thorsten
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

ghostanime2001
Posts: 402
Joined: Fri May 20, 2011 9:41 am

Postby ghostanime2001 » Fri Mar 08, 2013 10:43 am

in the context of LaTeX ?

User avatar
localghost
Site Moderator
Posts: 9204
Joined: Fri Feb 02, 2007 12:06 pm

Postby localghost » Fri Mar 08, 2013 10:47 am

ghostanime2001 wrote:in the context of LaTeX ?

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:[…] What is the difference between ifthen package and xparse package?

Reading the respective manuals should show you the differences.
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

User avatar
Stefan Kottwitz
Site Admin
Posts: 9582
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Fri Mar 08, 2013 11:12 am

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 etoolbox manual, 3.5 Boolean Flags.

Stefan
LaTeX.org admin

User avatar
cgnieder
Site Moderator
Posts: 1993
Joined: Sat Apr 16, 2011 7:27 pm

Postby cgnieder » Fri Mar 08, 2013 11:45 am

ghostanime2001 wrote:What is boolean anyway ?


Like the website Thorsten links to explains a boolean is a data-type that can have two different values, 0 and 1, say, or more often and semantically more meaningful false and true.

ghostanime2001 wrote:What or why is ifthen package useful ? And what is it used for ?

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 macro \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.

ghostanime2001 wrote:What is the difference between ifthen package and xparse package?

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's \newcommand.

ghostanime2001 wrote:In the xifthen package how do you use \newboolean ?


I'll show an example not for xifthen but for etoolbox and its \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:

  1. \documentclass{article}
  2. \usepackage[T1]{fontenc}
  3. \usepackage{lmodern}
  4.  
  5. \usepackage{xcolor}
  6. \colorlet{print}{black}
  7. \colorlet{screen}{blue}
  8.  
  9. \usepackage{etoolbox}
  10. % new boolean flag:
  11. \newbool{print}
  12. % it is initially set to `false' and can be set by issuing
  13. % \booltrue{print} or \boolfalse{print}
  14. % its value can be tested with
  15. % \ifbool{print}{<true branch>}{<false branch>}
  16.  
  17. \newcommand*\name[1]{%
  18. \textsc{%
  19. \ifbool{print}
  20. {\textcolor{print}{#1}}
  21. {\textcolor{screen}{#1}}%
  22. }%
  23. }
  24.  
  25. % uncomment to see the difference:
  26. % \booltrue{print}
  27.  
  28. \usepackage{hyperref}
  29. \ifbool{print}{}
  30. {
  31. \hypersetup{
  32. colorlinks,
  33. allcolors = link
  34. }}
  35.  
  36. \begin{document}
  37.  
  38. \section{A section}\label{sec:section}
  39. Here is a name \name{ghostanime2001} and a reference to
  40. section~\ref{sec:section}.
  41.  
  42. \end{document}


Regards
site moderator & package author

ghostanime2001
Posts: 402
Joined: Fri May 20, 2011 9:41 am

Postby ghostanime2001 » Fri Mar 08, 2013 7:30 pm

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 ?

User avatar
cgnieder
Site Moderator
Posts: 1993
Joined: Sat Apr 16, 2011 7:27 pm

Postby cgnieder » Fri Mar 08, 2013 7:51 pm

ghostanime2001 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 ?

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 statements
  1. IF statement
  2. do something if statement is true
  3. ELSE
  4. do something if it is false
  5. FI

The syntax depends, of course. LaTeX syntax (not TeX syntax) typically is something like this:
  1. \ifsometest{<statement>}
  2. {<do something if statement is true>}
  3. {<do something if it is false>}

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 ?

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.

Regards
site moderator & package author

ghostanime2001
Posts: 402
Joined: Fri May 20, 2011 9:41 am

Postby ghostanime2001 » Fri Mar 08, 2013 9:56 pm

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).

  1. \documentclass[fleqn]{article}
  2. \usepackage[T1]{fontenc}
  3. \usepackage{fullpage}
  4. \usepackage{makebox}
  5. \usepackage[version=3]{mhchem}
  6. \usepackage{tikz}
  7. \usetikzlibrary{matrix,calc}
  8. \setlength{\parindent}{0pt}
  9. \setlength{\parskip}{0pt}
  10. \setlength{\mathindent}{0pt}
  11. \begin{document}
  12. \begin{tikzpicture}[nodes=draw,baseline=(m-1-1.base)]
  13. \matrix (m) [matrix of math nodes,ampersand replacement=\&]
  14. {
  15. \vphantom{.} \& \node (a) {\ce{CH3COOH <=>}}; \& \node (b) {\ce{CH3COO- +}}; \& {\ce{H+}} \\
  16. \node (c) {\ce{I}}; \& \node {0}; \& \node {1}; \& \node (d) {0.48}; \\
  17. \node (e) {\ce{C}}; \& \node {+x}; \& \node {-x}; \& \node {-x}; \\
  18. \ce{E} \& \node (f) {x}; \& \node (g) {1-x}; \& \node {0.48-x}; \\
  19. };
  20. \draw (a.north east) -- (f.south east);
  21. \draw (a.north west) -- (f.south west);
  22. \draw (b.north east) -- (g.south east);
  23. \draw (c.north west) -- (d.north east);
  24. \end{tikzpicture}
  25. \end{document}

User avatar
cgnieder
Site Moderator
Posts: 1993
Joined: Sat Apr 16, 2011 7:27 pm

Postby cgnieder » Sat Mar 09, 2013 5:28 pm

It seems to me you want to create some kind of tabular layout. Why don't you simply use a {tabular}? Anyway, using the (A -| B) or (A |- B) syntax for coordinates you don't need to measure anything here.

  1. \documentclass{article}
  2. \usepackage[T1]{fontenc}
  3.  
  4. \usepackage[version=3]{mhchem}
  5. \usepackage{tikz}
  6. \usetikzlibrary{matrix,calc}
  7. \usepackage{array}
  8.  
  9. \begin{document}
  10. \begin{tikzpicture}[baseline=(m-1-1.base)]
  11. \matrix (m) [matrix of math nodes,ampersand replacement=\&]
  12. {
  13. \vphantom{.} \& \node (a) {\ce{CH3COOH <=>}}; \& \node (b) {\ce{CH3COO- +}}; \& {\ce{H+}} \\
  14. \node (c) {\ce{I}}; \& \node {0}; \& \node {1}; \& \node (d) {0.48}; \\
  15. \node (e) {\ce{C}}; \& \node {+x}; \& \node {-x}; \& \node {-x}; \\
  16. \ce{E} \& \node (f) {x}; \& \node (g) {1-x}; \& \node (h) {0.48-x}; \\
  17. };
  18. \draw (a.north east) -- (f.south east -| a.north east);
  19. \draw (a.north west) -- (f.south west -| a.north west);
  20. \draw (a.north east -| b.north east) -- (g.south east -| b.north east);
  21. \draw (c.north west) -- (d.north east -| h.south east);
  22. \end{tikzpicture}
  23.  
  24. \begin{tikzpicture}[baseline=(m-1-1.base)]
  25. \matrix (m) [nodes={minimum height=1.5em},matrix of math nodes,ampersand replacement=\&]
  26. {
  27. \& \ce{CH3COOH <=>} \& \ce{CH3COO- +} \& \ce{H+} \\
  28. \ce{I} \& 0 \& 1 \& 0.48 \\
  29. \ce{C} \& +x \& -x \& -x \\
  30. \ce{E} \& x \& 1-x \& 0.48-x \\
  31. };
  32. \draw (m-1-1.south west) -- (m-1-4.south east -| m-4-4.east);
  33. \draw (m-1-2.north west) -- (m-4-2.south west -| m-1-2.west);
  34. \draw (m-1-3.north west) -- (m-4-3.south west -| m-1-3.west);
  35. \draw (m-1-2.north -| m-4-4.south west) -- (m-4-4.south west);
  36. \end{tikzpicture}
  37.  
  38. \begin{tabular}{>{$}c<{$}*{3}{|>{$}c<{$}}}
  39. & \ce{CH3COOH <=>} & \ce{CH3COO- +} & \ce{H+} \\\hline
  40. \ce{I} & 0 & 1 & 0.48 \\
  41. \ce{C} & +x & -x & -x \\
  42. \ce{E} & x & 1-x & 0.48-x
  43. \end{tabular}
  44.  
  45. \end{document}


(IMHO the table would be just as clear and even more readable if you omitted all the lines.)

Regards
site moderator & package author


Return to “General”

Who is online

Users browsing this forum: No registered users and 7 guests