Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

\Atom{}{90/west/0,270/east/1}

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

\AtomSquash{}{90/west/sq,270/east/sq}

The code for creating the atoms is as follows

\newcommand{\Atom}[3][AtomNode]{
  \node[atomcore] (#1){#2};% {\ce{#2}};
  \foreach \ang/\anchor/\n in {#3} {
      \orbital{\ang}{#1.\anchor}{\n}
  }
}
\newcommand{\AtomSquash}[3][AtomNode]{
        \node[atomcore] (#1){#2};
        \foreach \ang/\anchor/\squashness in {#3}{
                \squashorbital{\ang}{#1.\anchor}{\squashness}
        }

}

And the orbital code:

\newcommand{\orbital}[3]{
    \begin{scope}[rotate=#1,shift=(#2)]
        % 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);
  \end{pgfonlayer}

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

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.

Help?

share|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

2 Answers 2

up vote 14 down vote accepted

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:

\ifthenelse{\equal{#3}{sq}}
           {... 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 ...}
share|improve this answer

With TeX primitives you can do something like

\def\sqstr{sq}
\def\argiii{#3}
\ifx\argiii\sqstr
  % #3==sq code
\else
  % #3!=sq code
\fi

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

share|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 S-Z 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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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