5

How do I do an inline if-else statement?

\draw[draw = #1, fill = \if black#1 white \else black \fi, line width = 0.1mm]
  (#2, #3) circle [radius = 0.25cm];

I've tried to do it based on this answer, for example, but with no success:

\begingroup
  \def\color{#1}
  \def\black{black}

  \draw[draw = #1, fill = \ifx\color\black white \else black \fi, line width = 0.1mm]
    (#2, #3) circle [radius = 0.25cm]; % chktex 1
\endgroup

I did try many other methods but none seem to work.

Here is a minimal complete example:

\documentclass{article}

\usepackage{tikz}

% Parameters
%
% 1: dimension (in cm)
% 2: board size (square)
% 
% Example: A 19x19 board with size 10cm x 10cm: `\gogrid{10}{19}'
\newcommand{\goGrid}[2]{
  \pgfmathsetmacro{\step}{#1 / (#2 - 1)} % chktex 1

  \draw[step=\step] (0, 0) grid (#1, #1);
}

\newcommand{\drawStone}[3]{
  \draw[draw = #1, fill = \if black#1 white \else black \fi, line width = 0.1mm]
    (#2, #3) circle [radius = 0.25cm];
}

\begin{document}
  \begin{tikzpicture}
    \goGrid{10}{19}

    \drawStone{white}{2 * 10cm / 18}{3 * 10cm / 18}
  \end{tikzpicture}
\end{document}

(Why are there dozens of ways of doing if statements in TeX but all of them seem super finicky? T.T)

2
  • 1
    \if bl is comparing b and l so is always false Feb 6 at 14:29
  • 1
    ` \def\color{#1}... would have worked but don't use \color` or you will disable latex color support. also don't use \beingroup around the \draw Feb 6 at 14:33

4 Answers 4

5

You can borrow a conditional from the L3 layer:

\documentclass{article}

\usepackage{tikz}

% Parameters
%
% 1: dimension (in cm)
% 2: board size (square)
% 
% Example: A 19x19 board with size 10cm x 10cm: `\gogrid{10}{19}'
\newcommand{\goGrid}[2]{
  \pgfmathsetmacro{\step}{#1 / (#2 - 1)} % chktex 1

  \draw[step=\step] (0, 0) grid (#1, #1);
}

\ExplSyntaxOn
\cs_new_eq:NN \myIfStrEqTF \str_if_eq:nnTF
\ExplSyntaxOff

\newcommand{\drawStone}[3]{
  \draw[draw = #1, fill = \myIfStrEqTF{black}{#1}{white}{black}, line width = 0.1mm]
    (#2, #3) circle [radius = 0.25cm];
}

\begin{document}
  \begin{tikzpicture}
    \goGrid{10}{19}

    \drawStone{white}{2 * 10cm / 18}{3 * 10cm / 18}
  \end{tikzpicture}
\end{document}
6
  • What's L3 Layer?
    – psygo
    Feb 6 at 14:44
  • 2
    @PhilippeFanaro the LaTeX3 programming layer built into the LaTeX2e kernel. Basically the stuff happening between \ExplSyntaxOn and \ExplSyntaxOff.
    – Skillmon
    Feb 6 at 14:45
  • Is there no real native way of doing this? This sounds like something that should be so simple... Is there no way of doing this with the ifthen package maybe?
    – psygo
    Feb 6 at 14:46
  • 4
    It doesn't get more "native" than with kernel methods (except with primitives only, but there is nothing handy there for this task, except building something that in the end turns out as \str_if_eq:nnTF or similar). And there is no better way doing this with ifthen. You want a check that's fully expandable at that place, which is not provided by ifthen. And everything happening in L3 is still TeX code, just more structured names and syntax, that's all.
    – Skillmon
    Feb 6 at 14:50
  • I see. Strange that there's no simpler way of doing this then. Thank you for your answers and comments. Do you have a reference as to where I could learn more about these \cs_new_eq:NN and \str_if_eq:nnTF?
    – psygo
    Feb 6 at 14:56
5

Much simpler: -black is white and conversely.

\documentclass{article}

\usepackage{tikz}

% Parameters
%
% 1: dimension (in cm)
% 2: board size (square)
%
% Example: A 19x19 board with size 10cm x 10cm: `\gogrid{10}{19}'
\newcommand{\goGrid}[2]{%
  \pgfmathsetmacro{\step}{#1 / (#2 - 1)} % chktex 1
  \draw[step=\step] (0, 0) grid (#1, #1);%
}

\newcommand{\drawStone}[3]{% -#1 is the complementary color
  \draw[draw = #1, fill = -#1, line width = 0.1mm]
    (#2*\step, #3*\step) circle [radius = 0.25cm];
}

\begin{document}
  \begin{tikzpicture}
    \goGrid{10}{19}
    \drawStone{white}{2}{3}
    \drawStone{black}{4}{5}
  \end{tikzpicture}
\end{document}

I also simplified \drawStone.

enter image description here

2
  • Is that some sort of coincidence or trick? Or are colors defined in some sort of wheel (maybe it's something to do with complements?)?
    – psygo
    Feb 6 at 17:43
  • 1
    @PhilippeFanaro It's a standard feature of xcolor. Depending on the color model the given color is defined within, -<color> will do the right thing.
    – egreg
    Feb 6 at 22:43
3

You don't really need a test here, just select the complementary color

\documentclass{article}

\usepackage{tikz}

\newcommand\notwhite{black}
\newcommand\notblack{white}
% Parameters
%
% 1: dimension (in cm)
% 2: board size (square)
% 
% Example: A 19x19 board with size 10cm x 10cm: `\gogrid{10}{19}'
\newcommand{\goGrid}[2]{%
  \pgfmathsetmacro{\step}{#1 / (#2 - 1)} % chktex 1
%
  \draw[step=\step] (0, 0) grid (#1, #1);%
}

\newcommand{\drawStone}[3]{%
  \draw[draw = #1, fill = \UseName{not#1}, line width = 0.1mm]
    (#2, #3) circle [radius = 0.25cm];
}

\begin{document}
  \begin{tikzpicture}
    \goGrid{10}{19}

    \drawStone{white}{2 * 10cm / 18}{3 * 10cm / 18}
  \end{tikzpicture}
\end{document}
8
  • Interesting. Didn't know this was possible!
    – psygo
    Feb 6 at 16:26
  • do you have a reference for this \UseName?
    – psygo
    Feb 6 at 16:53
  • 1
    @PhilippeFanaro texdoc userguide the main user level documentation in each latex release (section 4, page 17) Feb 6 at 16:58
  • 1
    @PhilippeFanaro perhaps you have an old latex release see section 4 of the current version at texdoc.org/serve/usrguide/0 Feb 6 at 17:33
  • 1
    @PhilippeFanaro The code was added earlier (2022) and if you do texdoc ltnews you will see it in ltnews 35. Feb 6 at 17:41
3

A more cumbersome way of doing the trick where not so many new things are used could be:

\documentclass{article}

\usepackage{tikz}

% Parameters
%
% 1: dimension (in cm)
% 2: board size (square)
% 
% Example: A 19x19 board with size 10cm x 10cm: `\gogrid{10}{19}'
\newcommand{\goGrid}[2]{
  \pgfmathsetmacro{\step}{#1 / (#2 - 1)} % chktex 1

  \draw[step=\step] (0, 0) grid (#1, #1);
}

% Paraphernalia ;-)
\newcommand\Exchange[2]{#2#1}%
\newcommand\FirstOfTwo[2]{#1}%
\newcommand\SecondOfTwo[2]{#2}%

\newcommand{\drawStone}[3]{%
  \begingroup
  \edef\tempa{\unexpanded{#1}}%
  \def\tempb{black}%
  \expandafter\endgroup
  \ifx\tempa\tempb\expandafter\FirstOfTwo\else\expandafter\SecondOfTwo\fi
  {\Exchange{white}}%
  {\Exchange{black}}%
  {\draw[draw = #1, fill = }, line width = 0.1mm]
    (#2, #3) circle [radius = 0.25cm];
}

\begin{document}
  \begin{tikzpicture}
    \goGrid{10}{19}

    \drawStone{white}{2 * 10cm / 18}{3 * 10cm / 18}
  \end{tikzpicture}
\end{document}

(Why are there dozens of ways of doing if statements in TeX but all of them seem super finicky? T.T)

They seem super finicky because the programming paradigm of the typesetting language TeX is different from the programming paradigms of most programming languages people are used to.

When learning TeX/LaTeX, don't try to transfer knowledge about programming in other languages (e.g., C++ or Java or Scheme) to TeX/LaTeX at too early a learning stage.
At the beginnings stick to the terminology introduced in Donald E. Knuth's TeXbook. That terminology is brought up because the concepts underlying TeX differ and thus their description/explanation requires different vocabulary.
The transfer of knowledge about programming in other languages can be done when you know how TeX works and thus know how to do the transfer properly.
E.g., you need to get familiar with concepts like "token"/"tokenization" and "expansion". And you need to get familiar with the fact that in TeX different levels of execution are intertwined - a fact which in the TeXbook is brought to the attention of learners by means of an analogy where the program TeX is seen as a beast which has eyes and a digestive tract with different stages of digestion (mouth, gullet, stomach, ...).

7
  • Cool! Do you think Knuth's TeXbook is the best reference book for this kind of thing? Do you have any other references you could give me? I've been looking for good references on TeX for quite a while, but all I seem to find seems so basic and superficial or super dated.
    – psygo
    Feb 6 at 16:57
  • 1
    @PhilippeFanaro LaTeX 2e (plus the layer 3-additions) actually is a collection of routines written in the language TeX and bunched together as a so-called format so that these routines are loaded and available by default without the user needing to provide extra instructions for loading them when running TeX. The TeXbook is a good reference (but one where information is provided in a rather condensed fashion so that every syllable in a sentence is relevant) when it comes to understanding basic concepts of how all TeX engines process .tex-input/.tex-files. Feb 6 at 18:31
  • 1
    E.g., if you like to grasp basic consepts of expansion and macro programming, which apply with all flavors of TeX, the chapter "Definitions (also called macros)" might be of interest. However, also a lot of information is conveyed in that book, which you usually don't need when just writing a document in LaTeX 2e, using one of the standard documentclasses. Besides, the The TeXbook does not document those routines that make up the format LaTeX 2e and which often are to prefer to the plain/primitive TeX control sequences when writing code using LaTeX 2e. Feb 6 at 18:31
  • 1
    In order to learn about the routines that make up the TeX format LaTeX 2e, you can look at The LaTeX Companion (3rd edition is current - its february 6, 2024) and at the commented sources of LaTeX 2e (source2e.pdf). The latter, however, requires a good understanding of TeX/of what is written in Knuth's TeXbook. However, the best source of information, in my humble opinion, still is a living TeX community where you can ask questions - e.g., here at TeX LaTeX StackExchange and in usenet newsgroups like comp.text.tex (english language) or de.comp.text.tex (german language). Feb 6 at 18:32
  • 1
    @PhilippeFanaro You are welcome. That's the second edition. I have the third edition in mind: https://www.informit.com/store/latex-companion-parts-i-ii-3rd-edition-9780138166489?ranMID=24808 / see also the web page of The LaTeX Project. There you can also learn about the team/the people who develop LaTeX. Many of them are regulars at TeX LaTeX StackExchange. In the past many of them used to be regulars at the usenet newsgroup comp.text.tex. ;-) Feb 6 at 18:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .