4

Last month, I asked this question, about how to personalize a flowchart based on data available in a text file. The idea was to be able to adjust the width of arrows depending on numbers in the text file. Now I have been wanting to go one step further, which is: not plotting the arrow in case the corresponding number in the data file is zero.

I managed to introduce a conditional to test whether my data is larger than zero, which works if I use an arbitrary number (e.g. \ifnum2>0), but not if I use my custom-defined command for fetching the data (e.g. \ifnum\flowwidth{A2B}>0, see below in the code chunk)

I have tried a few different options to try to make it work. Here is the base code, on which I started to make modifications (some parts come from the answer to this other question I asked last month):

\documentclass[tikz]{standalone}
\usepackage{readarray}
\usetikzlibrary{arrows,positioning}
\usepackage{filecontents}

\begin{filecontents*}{flow.txt}
A2B, 110$\pm$6, 1.5 mm
A2C, 0$\pm$0, 0 mm
A2D, 110$\pm$6, 0.4 mm
\end{filecontents*}

\begin{document}

% USE readarray  TO GET THE DATA INTO A \def
\readarraysepchar{;}
\readdef{flow.txt}\myflowdef

% USE listofitems TO DIGEST THE \def APPROPRIATELY
\setsepchar{;/,}
\ignoreemptyitems
\readlist*\myflowdata{\myflowdef}
\foreachitem\i\in\myflowdata[]{%
  \expandafter\edef\csname\myflowdata[\icnt,1]var\endcsname{\myflowdata[\icnt,2]}%
  \expandafter\edef\csname\myflowdata[\icnt,1]width\endcsname{\myflowdata[\icnt,3]}%
}

% For importing data
\newcommand{\flowvar}[1]{\csname#1var\endcsname}
\newcommand{\flowwidth}[1]{\csname#1width\endcsname}

\begin{tikzpicture}[BOX/.style   ={rectangle, draw, text centered},
                    ARROW/.style ={draw,-latex', line width = 0.5mm}]

\node [BOX] (a) {A};   
\node [BOX, right= of a] (c) {C};   
\node [BOX, above= of c] (b) {B};

\draw [draw,-stealth, line width = \flowwidth{A2B}] (a.north) |- (b.west);
\draw [draw,-stealth, line width = \flowwidth{A2C}] (a.east) -- (c.west);  

\end{tikzpicture}
\end{document}

I have tried to store the width in an intermediate variable instead of having the command after the if:

\def\myvar{\flowwidth{A2B}}
\ifnum\myvar>0%
    \draw [draw,-stealth, line width = \flowwidth{A2B}, color = red] (a.north) |- (b.west);
\fi

Or use \pgfmathsetmacro :

\pgfmathsetmacro\myvar{\csname\flowwidth{A2B}\endcsname}
\ifnum\myvar>0%
    \draw [draw,-stealth, line width = \flowwidth{A2B}, color = red] (a.north) |- (b.west);
\fi

Or use \expandafter :

\expandafter\ifnum\expandafter\flowwidth{A2B}>0%
    \draw [draw,-stealth, line width = \flowwidth{A2B}, color = red] (a.north) |- (b.west);
\fi

But those approaches yield nothing. I have also tried to play around with \csname and \endcsname. I have to admit that I made these changes rather randomly, with no true understanding of the programming principles behind. I would be very glad if someone would have suggestions on how to solve this and also if anyone knows a good resource/tutorial that explains that sort of LaTeX programming.

  • The test for \flowwidth should be \ifdim\flowwidth{A2B}>0pt, because the variable contains a length. – egreg Oct 20 '17 at 10:49
  • That does indeed do the trick. I was looking way too far it seems. Thank you very much! – Delphine Oct 20 '17 at 11:22
1

The final expansion of \flowwidth{A2B} is 1.5mm, so a test for it being positive should use \ifdim:

\ifdim\flowwidth{A2B}>0pt

(remember to leave a space, or end-of-line, after pt in order to avoid untimely expansion of what follows).

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.