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

share|improve this question
    
You will need to define the test condition for clarity in your question, what is #1 and #2? –  Yiannis Lazarides Oct 16 '11 at 19:12
    
@Yiannis Lazarides: just made it clearer... –  SoftTimur Oct 16 '11 at 23:34
add comment

1 Answer 1

up vote 2 down vote accepted

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}
share|improve this answer
    
Thanks... one thing is... it seems that ifx checks only numbers, but I often have for instance \node[my funny rectangle={$i$}{$i$}{4}{8}] {text};, do you have any idea? –  SoftTimur Oct 16 '11 at 17:59
1  
@SoftTimur: Is that version going to work with the main code? Since the numbers are used also as coordinates then I would be a little surprised to discover that $i$ was an acceptable argument. The definition might need a little reworking if you want to label the corners by something other than their coordinates. –  Loop Space Oct 16 '11 at 18:02
    
@SoftTimur: As Andrew mentioned, the problem is not so much with \ifx, but rather with the use of the arguments to specify the text block size/dimension, and $i$ has no number value. –  Werner Oct 16 '11 at 18:15
1  
So then \def\argone{#1}\def\argtwo{#2} ... \ifx\argone\argtwo ought to do it, shouldn't it? –  Loop Space Oct 17 '11 at 6:54
1  
Putting the assignment in \pgfextra works for me. Note that the assignment \let\mainnode=\tikzlastnode is in the \pgfextra so there's no grouping issues there. What really confused me is that SoftTimur put the doubled argument on the big square, not the little one! So I thought that the conditional was being evaluated the wrong way around. –  Loop Space Oct 17 '11 at 7:49
show 8 more comments

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.