3

When declaring shapes, I usually define the desired shape width with:

minimum width = <number> <measure units>

However, sometimes, I calculate calculate the shape's width separately. In that case, one should use

minimum width = <length>

My questions is: is it possible, in tikz, to preset shape parameters such that it be automatically assigned a minimum width regardless of whether it be specified as a number or as a length?

The following MWE, I hope, will clarify my question.

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{calc,positioning}

\begin{document}
    \begin{tikzpicture}[node distance=7mm and 0mm,
N/.style 2 args = {name=n#1,
                   minimum width=#2 mm,%<-- how to change this declaration in a wex
                                       %    when #2 contain length, the following
                                       %    units  (mm) are omitted?
                   shape=rectangle, draw},
                        ]
    \node[N={1}{22}]                    {node 1};
\dimendef\nodewidth=0
\pgfmathsetlength{\nodewidth}{22 mm}
%    \node[N={2}{\nodewidth},% <-- this gives expected error!
    \node[N={2}{},minimum width=\nodewidth,% <--- this is little bit inconvenient
          below right=of n1.south west] {node 2};
%-------
    \end{tikzpicture}
\end{document}

I imagine that some test on #2 should be performed when the minimum width is preset, but I do not know how to do this.

6
  • Please make your code compilable (if possible), or at least complete it with \documentclass{...}, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem.
    – jub0bs
    Dec 19 '13 at 18:27
  • I do not understand why you want the mm unit to be hardcoded in the macro. Is there a specific reason? Otherwise you could just have minimum width=#2, and \node[N={1}{22mm}] and \node[N={2}{\nodewidth}
    – Bordaigorl
    Dec 19 '13 at 18:47
  • you are right. i'm aware of this possibilities, but i have a lot old pictures, where i use declaration as it is showed in the mwe. now it seems, that in time, when i have decided for this form of writing of units, i was lazy to write mm again and again ... now i have a (big) dilema, what is better, to change all old pictures (few hundreds of them) and with this solve the problem and also maybe make picture codes more clear or with help with TeX-perts here a solution as i ask for.
    – Zarko
    Dec 19 '13 at 19:11
  • Hi Zarko. Out of interest, are you currently working on the wikibook in your language? If so, can you come into chat and ping me? It is somehow related to How can we, as a community, improve the LaTeX WikiBook?
    – Johannes_B
    Feb 1 '16 at 16:37
  • @Johannes_B, I'm not working on wikibook in my language, but I'm interested to start with this. Please tel me, how to activate a chat here (on SE)? I'm not familiar with it, please help me :-)
    – Zarko
    Feb 1 '16 at 16:45
4

I’d use \ifpgfmathunitsdeclared to test whether #2 had any units.

If units were present, use the result directly (with pt which aren’t included in \pgfmathresult). Otherwise append mm.

Code

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{positioning}
\newdimen\nodewidth
\begin{document}
\begin{tikzpicture}[node distance=7mm and 0mm,
  N/.style 2 args = {
    name=n#1, draw,
    /utils/exec={%
      \pgfmathparse{#2}\ifpgfmathunitsdeclared
        \pgfkeysalso{/pgf/minimum width/.expanded={\pgfmathresult pt}}%
      \else
        \pgfkeysalso{/pgf/minimum width/.expanded={\pgfmathresult mm}}%
      \fi},
    shape=rectangle
  }]
\node[N={1}{22}] {node 1};

\pgfmathsetlength{\nodewidth}{22 mm}
\node[N={2}{\nodewidth}, below=of n1] {node 2};
\end{tikzpicture}
\end{document}
1
  • thank you very much! this work as i imagine that it should, but the key system is still over my ability to understand it and consequently use it. description in TikZ manual is to concise for me, i wish, that there will be more practical examples.
    – Zarko
    Dec 19 '13 at 21:18

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.