[Tex/LaTex] How to add a condition in \tikzset

conditionalsmacrostikz-pgf

I am trying to make some modification on the answer of Andrew Stacey of my previous question "A macro of drawing a rectangle with several parameters in TikZ".

The current code is the following:

\documentclass{article}
\usepackage[frenchb]{babel}
\usepackage{tikz}
\usetikzlibrary{fit}

\tikzset{
  my funny rectangle/.style n args={4}{%
    rectangle,
    draw,
    append after command={\pgfextra{\let\mainnode=\tikzlastnode}
      node[above right] at (\mainnode.north west) {#3}%
      node[above left] at (\mainnode.north east) {#4}%
      node[below left] at (\mainnode.north west) {#1}%
      node[above left] at (\mainnode.south west) {#2}%
    },
  }
}

\begin{document}
\begin{tikzpicture}
\node[my funny rectangle={1}{7}{4}{8}] {a text};
\node[my funny rectangle={$i$}{$i$}{4}{8}, text width={3cm}, minimum height={3cm}, text centered] (abc) at (5,5) {text};
\end{tikzpicture}
\end{document}

I would like to add a condition inside the append after command: if #1 = #2, do only node[below left] at (\mainnode.west) {#1}, else do node[below left] at (\mainnode.north west) {#1} and node[above left] at (\mainnode.south west) {#2}.

The answer that @Werner suggested works fine for arguments like {1}{7}{4}{8}, but does not work for arguments like {$i$}{$i$}{4}{8}. Does anyone have an idea?

Best Answer

This is probably what you're after:

enter image description here

The standard TeX conditional \ifx#1#2 <true> \else <false> \fi works in this case, executing <true> if #1=#2 and <false> otherwise.

\documentclass{standalone}
%\url{http://tex.stackexchange.com/q/27278/86}
\usepackage{tikz}
\usetikzlibrary{fit}

\tikzset{
  my funny rectangle/.style n args={4}{%
    rectangle,
    draw,
    fit={(#3,#1) (#4,#2)},
    append after command={\pgfextra{\let\mainnode=\tikzlastnode}
      node[above right] at (\mainnode.north west) {#3}%
      node[above left] at (\mainnode.north east) {#4}%
      \ifx#1#2
        node[left] at (\mainnode.west) {#1}%
      \else
        node[below left] at (\mainnode.north west) {#1}%
        node[above left] at (\mainnode.south west) {#2}%
      \fi
    },
  }
}

\begin{document}
\begin{tikzpicture}
  \node[my funny rectangle={1}{1}{4}{8}] {text};
\end{tikzpicture}
\end{document}

Edit: Here's the updated code that addresses passing some non-numeric arguments to my funny rectangle. With the node no longer fitted, you need to specify additional width and height parameters:

enter image description here

\documentclass{article}
\usepackage{tikz}% http://ctan.org/pkg/pgf
%\usetikzlibrary{fit}

\tikzset{
  my funny rectangle/.style n args={4}{%
    rectangle,
    draw,
    %fit={(#3,#1) (#4,#2)},
    append after command={\pgfextra{%
      \let\mainnode=\tikzlastnode%
      \def\argone{#1}\def\argtwo{#2}}
      node[above right] at (\mainnode.north west) {#3}%
      node[above left] at (\mainnode.north east) {#4}%
      \ifx\argone\argtwo
        node[left] at (\mainnode.west) {#1}%
      \else
        node[below left] at (\mainnode.north west) {#1}%
        node[above left] at (\mainnode.south west) {#2}%
      \fi
    },
  }
}

\begin{document}
\begin{tikzpicture}
\node[my funny rectangle={$i$}{$i$}{4}{8}, text width={3cm}, minimum height={3cm}, text centered] (abc) at (5,5) {text};
\end{tikzpicture}
\end{document}