Generalcase-statement conditional text

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
Shea12Shea12
Posts: 6
Joined: Mon Feb 12, 2024 6:06 pm

case-statement conditional text

Post by Shea12Shea12 »

Hello,

In my continued search for various ways of creating conditional text, I'm looking for a good way to handle a case statements. One way of handling it is to use nested if statements, but the more cases your have the uglier/more-cumbersome it becomes, and I am also getting a weird spacing issue where the text is placed (large spacing after the colon).

Is there a smarter way of handling this?

Thank you for taking the time to read and reply,
Alan

Example nested statement, spacing issue

Code: Select all

\documentclass{article}
\usepackage{etoolbox}

\begin{document}
\newbool{customer1}
\newbool{customer2}
%...
\newbool{customerN}

This is general text all customers get.

\booltrue{customer1}
This is special information:
\ifboolexpr{bool{customer1}}{
customer1 specific information
}{
\ifboolexpr{bool{customer2}}{
customer2 specific information
}{
\ifboolexpr{bool{customer2}}{
customerN specific information
}{
No previous customers were matched
}}}

\boolfalse{customer1}
\booltrue{customerN}
Notice the larger spacing after the colon using customerN

This is special information:
\ifboolexpr{bool{customer1}}{
customer1 specific information
}{
\ifboolexpr{bool{customer2}}{
customer2 specific information
}{
\ifboolexpr{bool{customerN}}{
customerN specific information
}{
No previous customers were matched
}}}

\bigskip

If the text is on a new line, no spacing issue
\booltrue{customer1}
\boolfalse{customerN}

\ifboolexpr{bool{customer1}}{
customer1 specific information
}{
\ifboolexpr{bool{customer2}}{
customer2 specific information
}{
\ifboolexpr{bool{customer2}}{
customerN specific information
}{
No previous customers were matched
}}}

\boolfalse{customer1}
\booltrue{customerN}

\ifboolexpr{bool{customer1}}{
customer1 specific information
}{
\ifboolexpr{bool{customer2}}{
customer2 specific information
}{
\ifboolexpr{bool{customerN}}{
customerN specific information
}{
No previous customers were matched
}}}

\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

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

Re: case-statement conditional text

Post by Stefan Kottwitz »

Hi Alan,

that's an excellent test example! Here's the improved code:

Code: Select all

\documentclass{article}
\usepackage{etoolbox}

\begin{document}
\newbool{customer1}
\newbool{customer2}
%...
\newbool{customerN}
\boolfalse{customer1}
\booltrue{customerN}
This is special information:
\ifboolexpr{bool{customer1}}{%
customer1 specific information
}{%
\ifboolexpr{bool{customer2}}{%
customer2 specific information
}{%
\ifboolexpr{bool{customerN}}{%
customerN specific information
}{%
No previous customers were matched
}}}
\end{document}
In text, the end of a line is like a white space. So just take care that your macros don't insert line spaces just because the code line ends. Just comment out the line ending by a % where it should not cause a white space.

That's the reason why you often can see percent signs at the end of lines in longer LaTeX macros that generate text, to avoid generating spaces. After a while one gets used to it and ends every macro code line with a % because the "end of line space" is only needed in text.

Stefan
LaTeX.org admin
Shea12Shea12
Posts: 6
Joined: Mon Feb 12, 2024 6:06 pm

Re: case-statement conditional text

Post by Shea12Shea12 »

Thank you!
Post Reply