1

I want to import a value form an txt-File and use it in tikz. But for some reason this is not working (line 25). I was not able to find a workaround like the \protect-command which works for \caption or \section.

The error message is:

Illegal parameter number in definition of \tikz@scan@point@coordinate.

\documentclass[11pt,a4paper]{article}

%\usepackage{filecontents}
\begin{filecontents*}{text1.txt}
per = 10
\end{filecontents*}

\usepackage{tikz}
\usepackage{datatool}

\DTLsetseparator{ = }
\DTLloaddb[noheader, keys={thekey,thevalue}]{para}{text1.txt}
% Loads mydata.dat with column headers 'thekey' and 'thevalue'
\newcommand{\datapar}[1]{\DTLfetch{para}{thekey}{#1}{thevalue}}

\newcommand{\test}{10} % alternative implementation (for testing)

\begin{document}
insert value form text1.txt: \datapar{per}


\begin{figure}[!ht]
    \begin{center}
      \begin{tikzpicture}
        \draw (\datapar{per},0) -- (4,0); %value from text1.txt in tikz (not working)
        \draw (\test,0) -- (4,0); % same value from different variable is no problem.
      \end{tikzpicture}
      \caption{caption is working, too: \protect\datapar{per}}
    \end{center}
\end{figure}
\end{document}
0

1 Answer 1

1

The issue is that \DTLfetch retrieves and prints the value. But you do not want to print it, you want to use it for a computation. With pgf you do not need to create an expandable variant, it suffices to declare a function that retrieves the value and stores it into \pgfmathresult. I called the function datapar to match the name of your macro, but obviously you can create much more elaborate functions of this type.

\documentclass[11pt,a4paper]{article}

\begin{filecontents*}[overwrite]{text1.txt}
per = 10
\end{filecontents*}

\usepackage{tikz}
\usepackage{datatool}

\DTLsetseparator{ = }
\DTLloaddb[noheader, keys={thekey,thevalue}]{para}{text1.txt}
% Loads mydata.dat with column headers 'thekey' and 'thevalue'
\newcommand{\datapar}[1]{\DTLfetch{para}{thekey}{#1}{thevalue}}
\pgfmathdeclarefunction{datapar}{1}{%
\edtlgetrowforvalue{para}{\dtlcolumnindex{para}{thekey}}{#1}%
\dtlgetentryfromcurrentrow{\pgfmathresult}{\dtlcolumnindex{para}{thevalue}}%
}
\newcommand{\test}{10} % alternative implementation (for testing)
%\show\DTLfetch
\begin{document}
insert value form text1.txt: \datapar{per}


\begin{figure}[!ht]
    \begin{center}
      \begin{tikzpicture}
        \draw ({datapar("per")},0) -- (4,0); %value from text1.txt in tikz (not working)
        \draw[red,dashed] (\test,0) -- (4,0); % same value from different variable is no problem.
      \end{tikzpicture}
      \caption{caption is working, too: \protect\datapar{per}}
    \end{center}
\end{figure}
\end{document}

enter image description here

I added the second line red and dashed to be able to confirm that both lines have the same start and end points, as they should.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .