1

I write a program that typesets elements in a Tikz picture. In general the items are not placed correctly. In order to solve this, I was thinking of running a two-stage compiler: the first time the nodes are rendered using Tikz and the dimensions (width and height) are measured. The results are written to a file and the a constraint logic programming solver (ECLiPSe) aims to find a better orientation for the nodes. Using different approaches, writing the size to the file still doesn't work:

Minimal Working Example (MWE):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit,patterns,snakes,calc,decorations,decorations.text,positioning,decorations.pathreplacing,matrix,arrows,automata}
%measure script
\makeatletter
\newcommand\nodedim[1]{
    \pgfextractx{\nwdt}{\pgfpointanchor{#1}{east}}%
    \pgfextractx{\pgf@xa}{\pgfpointanchor{#1}{west}}% \pgf@xa is a length defined by PGF for temporary storage. No need to create a new temporary length.
    \addtolength{\nwdt}{-\pgf@xa}%
    \pgfextracty{\nhgt}{\pgfpointanchor{#1}{south}}%
    \pgfextracty{\pgf@xa}{\pgfpointanchor{#1}{north}}% \pgf@xa is a length defined by PGF for temporary storage. No need to create a new temporary length.
    \addtolength{\nhgt}{-\pgf@xa}%
}
\makeatother

\newlength\nwdt
\newlength\nhgt

\begin{document}

%open the file
\newwrite\specfile
\immediate\openout\specfile=spec.pl
%draw node
\begin{tikzpicture}
\node (foo) at (0,0) {This is a test node};
%now measure the node
\nodedim{foo}

\immediate\write\specfile{rectangle(\showthe\nwdt,\showthe\nhgt).}
\end{tikzpicture}
%close the file
\immediate\closeout\specfile
\end{document}

The aim is that spec.pl should show for instance:

rectangle(14,4).

with 14 and 4 respectively the width and the height of the node.

Evidently the commands are later wrapped around a macro to generate a full list of rectangle(...). items.

4
  • Your MWE is not complete (no class, no document)... Apr 7, 2015 at 13:01
  • @PaulGaborit: better? I considered it to be noise to add additional (bit trivial) commands, since it shifts the attention away from the vital parts. Apr 7, 2015 at 13:05
  • Use \the instead of \showthe... Apr 7, 2015 at 13:12
  • @PaulGaborit: yes! that's it! Many thanks. If you write a short answer, I will accept it. Can you explain what the difference is between \the and \showthe? Apr 7, 2015 at 13:15

1 Answer 1

3

Use \the instead of \showthe.

The \showthe command will display on the log file and terminal the tokens that \the produces. - Extract from "TeX by topic"

Edit:

You may use append after command, let (from calc TikZ library) and \pgfmathsetmacro to simplify the code and to choose the unit:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\tikzset{
  write dim/.style={
    append after command={
      let \p1=(\tikzlastnode.south west), \p2=(\tikzlastnode.north east) in
      \pgfextra{
        \pgfmathsetmacro\myw{(\x2-\x1)/1cm}
        \pgfmathsetmacro\myh{(\y2-\y1)/1cm}
        \immediate\write\specfile{rectangle(\myw,\myh)}
      }
    },
  }
}

\begin{document}

%open the file
\newwrite\specfile
\immediate\openout\specfile=spec.pl

\begin{tikzpicture}
  \node[write dim] (foo) at (0,0) {This is a test node};
\end{tikzpicture}

%close the file
\immediate\closeout\specfile
\end{document}
1
  • An additional question: is there a way to modify the unit (pt) in which the length is expressed, for instance cm? Apr 7, 2015 at 14:03

You must log in to answer this question.

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