I'm having trouble writing if statements, I can find next to no documentation on them and what I want to do seems quite simple.

I'm using the orbitals pgf/tikz example from here, and I'd like to write a new atom type, whereby instead of the third argument, it takes phrase describing squashedness

Normally, the code to create an atom looks like this:


So I wrote a separate method which I'm trying to modify that


The code for creating the atoms is as follows

  \node[atomcore] (#1){#2};% {\ce{#2}};
  \foreach \ang/\anchor/\n in {#3} {
        \node[atomcore] (#1){#2};
        \foreach \ang/\anchor/\squashness in {#3}{


And the orbital code:

        % These points define the curve for the orbital.
        \coordinate (c1) at (-\orbwidth, .6 * \orbheight);
        \coordinate (c2) at (-\orbwidth, \orbheight);
        \coordinate (c3) at (\orbwidth, \orbheight);
        \coordinate (c4) at (\orbwidth, .6 * \orbheight);
        \coordinate (top) at (0,\orbheight);

        %Coordinates of the electrons

        \coordinate (e1) at (0, 0.45*\orbheight);
        \coordinate (e2) at (0, 0.75*\orbheight);
    \end{scope}  \begin{pgfonlayer}{background}

      \draw[orbital #3] (#2) .. controls (c1) and (c2) .. (top) ..
            controls (c3) and (c4) .. (#2);

  % Draw the electrons
      \foreach \n in {1,...,#3} {
          %Don't need electrons to be shaded.
          %\shade[ball color=electron] (e\n) circle (1mm);

What I would especially like to do here, is have the \atom and \atomsquash in a single method, which would decide based on the number of arguments or say the text value of the third argument (is it equal to "sq" or is it a number?). I've tried writing an if statement that looks like the following \iftype=sq but the compiler seriously dislikes this. It doesn't help if I put quotes around the sq, the code will compile then but the if statement never is true.


| improve this question | | | | |
  • I don't quite understand what you mean by \iftype; this is not a defined control sequence as far as I know. Which code did compile? – Hendrik Vogt Oct 28 '10 at 8:47

You can't compare strings easily using plain TeX's \ifs. If you're using LaTeX, I would use the ifthen package instead, which you almost definitely have installed already. With it, your desired if statement looks like this:

           {... code if #3 == sq ...}
           {... code if #3 != sq ...}

You can also do numeric tests within the condition, so for instance

\ifthenelse{\equal{#3}{sq} \OR #3 > 0}
           {... valid ...}
           {... invalid ...}
| improve this answer | | | | |

With TeX primitives you can do something like

  % #3==sq code
  % #3!=sq code

I started out preferring ifthen.sty but for some reason nowadays I like TeX \ifs.

| improve this answer | | | | |
  • 1
    Although this may not be a problem, note that this will fail if #3 is itself a macro (e.g., \maybeSQ). You could use \edef\argiii{#3}, but that fails if there's anything unexpandable in #3. I also started out preferring ifthen, and then moved to TeX \if structures; (I think it's because the syntax is nicer, even if it does have some strange properties); however, string equality is one place where I always use \ifthenelse, for reasons like this. – Antal Spector-Zabusky Oct 27 '10 at 20:05
  • @Antal: I think I do the exact same thing. – Matthew Leingang Oct 27 '10 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.