4

I'm a bit baffled why the results of these two MWE should result in different output at all.

Example 1 (desired output)

In this example, I set the y-coordinate value using a length:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\setlength{\parindent}{0pt}
\newlength{\mydepth}%
\begin{document}

A

\begin{tikzpicture}
      \coordinate (top right) at (0,0);
      \setlength\mydepth{\dimexpr2in+2cm\relax}
      \coordinate (bottom left) at (4in,-\mydepth);

      \draw (bottom left) -- (top right);
      \node[draw] (A) at ($(top right)!0.5!(bottom left)$) {CONTENT};

      \draw[blue,line width=2pt] (current bounding box.south east) rectangle (current bounding box.north west);

\end{tikzpicture}

\end{document}

enter image description here

Example 2 (icky output)

In this example, I set the y-coordinate value using a macro:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\setlength{\parindent}{0pt}
\begin{document}

A

\begin{tikzpicture}
      \coordinate (top right) at (0,0);
      \edef\mydepth{\dimexpr2in+2cm\relax}
      \coordinate (bottom left) at (4in,-\mydepth);

      \draw (bottom left) -- (top right);
      \node[draw] (A) at ($(top right)!0.5!(bottom left)$) {CONTENT};

      \draw[blue,line width=2pt] (current bounding box.south east) rectangle (current bounding box.north west);

\end{tikzpicture}

\end{document}

enter image description here

Actually this 2nd example, I don't need the macro. Originally this issue arose with the following code:

\coordinate (bottom left) at (4in,\dimexpr2in+2cm\relax);

So the question isn't really one of macro vs length. It's a matter of calculating lengths in a coordinate.

5
  • with the second I get ! You can't use '\dimexpr' in restricted horizontal mode. and no tikz picture Oct 2 '13 at 0:01
  • @DavidCarlisle I get that error only if I place \edef\mydepth... outside the tikzpicture. Inside I get no such error. Do you get such an error with \coordinate (bottom left) at (4in,\dimexpr2in+2cm\relax);?
    – A.Ellett
    Oct 2 '13 at 0:03
  • Yes get error in that case too (Texlive 2013 version) I could post \listfiles output to my answer Oct 2 '13 at 0:08
  • @DavidCarlisle \listfiles might be good. I'm working on my laptop which has the cvs version from 8/23/2013. At home, I have a more recent cvs version (from late September) on my computer. I'll check things out at home too.
    – A.Ellett
    Oct 2 '13 at 0:10
  • This appeared to be a bug in the PGFmath parser. I've added an explanation as an answer below, and fixed the bug in the PGF CVS version. Oct 2 '13 at 8:05
6

Using

 \edef\mydepth{\dimexpr2in+2cm\relax}

I get a syntax error

! You can't use '\dimexpr' in restricted horizontal mode

and no output.

With

\edef\mydepth{\the\dimexpr2in+2cm\relax}

I get the same output as with

      \setlength\mydepth{\dimexpr2in+2cm\relax}

adding \listfiles to your second example I get an error then the list as shown. you might compare file versions with your setup:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\setlength{\parindent}{0pt}
\listfiles
\begin{document}

A

\begin{tikzpicture}
      \coordinate (top right) at (0,0);
      \edef\mydepth{\dimexpr2in+2cm\relax}
      \coordinate (bottom left) at (4in,-\mydepth);

      \draw (bottom left) -- (top right);
      \node[draw] (A) at ($(top right)!0.5!(bottom left)$) {CONTENT};

      \draw[blue,line width=2pt] (current bounding box.south east) rectangle (current bounding box.north west);

\end{tikzpicture}

\end{document}

! You can't use `\dimexpr' in restricted horizontal mode.
<argument> \dimexpr 

l.14 ...oordinate (bottom left) at (4in,-\mydepth)
                                                  ;
? 
! Undefined control sequence.
\pgfmath@dimen@ ...men@@ #1=0.0pt\relax \pgfmath@ 

l.14 ...oordinate (bottom left) at (4in,-\mydepth)
                                                  ;
? 
! Missing number, treated as zero.
<to be read again> 
                   \def 
l.14 ...oordinate (bottom left) at (4in,-\mydepth)
                                                  ;
? 
! Illegal unit of measure (pt inserted).
<to be read again> 
                   \def 
l.14 ...oordinate (bottom left) at (4in,-\mydepth)
                                                  ;
? 
Runaway argument?
\pgfmath@number {0}\expandafter \ifcat \pgfmath@token@next \relax \let \ETC.
! Paragraph ended before \pgfmath@dimen@@ was complete.
<to be read again> 
                   \par 
l.15 

? 

! Package pgf Error: No shape named bottom left is known.

See the pgf package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.16       \draw (bottom left)
                               -- (top right);
? 

! Package pgf Error: No shape named bottom left is known.

See the pgf package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.16       \draw (bottom left) -- (top right)
                                             ;
? 

! Package pgf Error: No shape named bottom left is known.

See the pgf package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.17 ...aw] (A) at ($(top right)!0.5!(bottom left)
                                                  $) {CONTENT};
? 
! Extra }, or forgotten \endgroup.
\endpgfpicture ... \pgf@x }\endgroup \hss \egroup 
                                                  \pgf@insertlayers \endgrou...
l.21 \end{tikzpicture}

? 

! LaTeX Error: \begin{tikzpicture} on input line 11 ended by \end{document}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.23 \end{document}

? 
! Missing \endgroup inserted.
<inserted text> 
                \endgroup 
l.23 \end{document}

? 
! Missing } inserted.
<inserted text> 
                }
l.23 \end{document}

? 

Overfull \hbox (62.53825pt too wide) detected at line 23
[][][][][] 
 []

[1

{/usr/local/texlive/2013/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
(./tkl111.aux)

 *File List*
 article.cls    2007/10/19 v1.4h Standard LaTeX document class
  size10.clo    2007/10/19 v1.4h Standard LaTeX file (size option)
    tikz.sty    2010/10/13 v2.10 (rcs-revision 1.76)
     pgf.sty    2008/01/15 v2.10 (rcs-revision 1.12)
  pgfrcs.sty    2010/10/25 v2.10 (rcs-revision 1.24)
everyshi.sty    2001/05/15 v3.00 EveryShipout Package (MS)
  pgfrcs.code.tex
 pgfcore.sty    2010/04/11 v2.10 (rcs-revision 1.7)
graphicx.sty    1999/02/16 v1.0f Enhanced LaTeX Graphics (DPC,SPQR)
  keyval.sty    1999/03/16 v1.13 key=value parser (DPC)
graphics.sty    2009/02/05 v1.0o Standard LaTeX Graphics (DPC,SPQR)
    trig.sty    1999/03/16 v1.09 sin cos tan (DPC)
graphics.cfg    2010/04/23 v1.9 graphics configuration of TeX Live
  pdftex.def    2011/05/27 v0.06d Graphics/color for pdfTeX
infwarerr.sty    2010/04/08 v1.3 Providing info/warning/error messages (HO)
 ltxcmds.sty    2011/11/09 v1.22 LaTeX kernel commands for general use (HO)
  pgfsys.sty    2010/06/30 v2.10 (rcs-revision 1.37)
  pgfsys.code.tex
pgfsyssoftpath.code.tex    2008/07/18  (rcs-revision 1.7)
pgfsysprotocol.code.tex    2006/10/16  (rcs-revision 1.4)
  xcolor.sty    2007/01/21 v2.11 LaTeX color extensions (UK)
   color.cfg    2007/01/18 v1.5 color configuration of teTeX/TeXLive
 pgfcore.code.tex
pgfcomp-version-0-65.sty    2007/07/03 v2.10 (rcs-revision 1.7)
pgfcomp-version-1-18.sty    2007/07/23 v2.10 (rcs-revision 1.1)
  pgffor.sty    2010/03/23 v2.10 (rcs-revision 1.18)
 pgfkeys.sty    
 pgfkeys.code.tex
  pgffor.code.tex
    tikz.code.tex
supp-pdf.mkii
pdftexcmds.sty    2011/11/29 v0.20 Utility functions of pdfTeX for LuaTeX (HO)
ifluatex.sty    2010/03/01 v1.3 Provides the ifluatex switch (HO)
   ifpdf.sty    2011/01/30 v2.3 Provides the ifpdf switch (HO)
epstopdf-base.sty    2010/02/09 v2.5 Base part for package epstopdf
  grfext.sty    2010/08/19 v1.1 Manage graphics extensions (HO)
kvdefinekeys.sty    2011/04/07 v1.3 Define keys (HO)
kvoptions.sty    2011/06/30 v3.11 Key value format for package options (HO)
kvsetkeys.sty    2012/04/25 v1.16 Key value parser (HO)
etexcmds.sty    2011/02/16 v1.5 Avoid name clashes with e-TeX commands (HO)
epstopdf-sys.cfg    2010/07/13 v1.3 Configuration of (r)epstopdf for TeX Live
 ***********

 )
(\end occurred inside a group at level 2)

### semi simple group (level 2) entered at line 12 (\begingroup)
### semi simple group (level 1) entered at line 11 (\begingroup)
### bottom level
(\end occurred when \iffalse on line 14 was incomplete)
(\end occurred when \ifx on line 14 was incomplete)
(\end occurred when \ifcat on line 14 was incomplete)
(\end occurred when \ifx on line 14 was incomplete)
(\end occurred when \iftrue on line 14 was incomplete)
(\end occurred when \iffalse on line 14 was incomplete) 
Here is how much of TeX's memory you used:
2
  • Two questions: why do you think I'm not getting a syntax error? And, when I tried \the\dimexpr.... things worked out as I expect them to. What's the \the doing that's making things work?
    – A.Ellett
    Oct 2 '13 at 0:07
  • @A.Ellett compare \listfiles with the version I just added, something is different:-) \the expands the length back to character tokens, if \dimen@ is 4.2pt, \the\dimen@ expands to 4.2pt similarly with \dimexpr which is essentially an anonymous length register. Oct 2 '13 at 0:13
5

The "icky" output is the result of a space being inserted due a missing % in the pgfmath parser code. This can be seen in the following:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newlength{\mydepth}%
\begin{document}

\hbox{\setlength\mydepth{\dimexpr0.2in+0.2cm}:\pgfmathparse{\mydepth}: \pgfmathresult,}

\hbox{\edef\mydepth{\dimexpr0.2in+0.2cm}:\pgfmathparse{\mydepth}: \pgfmathresult}

\end{document}

Which produces:

:: 20.14423,
: : 20.14423

The offending command was \pgfmath@parse@dimexpr@ which was lacking a % after its opening {:

\def\pgfmath@parse@dimexpr@{
  \edef\pgfmathresult{\pgfmath@tonumber{\pgfmath@dimen}}%
  \expandafter\pgfmath@stack@push@operand\expandafter{\pgfmathresult}%
  \pgfmath@parse@@operator%
}

By adding the % the space goes away:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newlength{\mydepth}%

\makeatletter
\def\pgfmath@parse@dimexpr@{%
  \edef\pgfmathresult{\pgfmath@tonumber{\pgfmath@dimen}}%
  \expandafter\pgfmath@stack@push@operand\expandafter{\pgfmathresult}%
  \pgfmath@parse@@operator%
}
\makeatother

\begin{document}


\hbox{\setlength\mydepth{\dimexpr0.2in+0.2cm}:\pgfmathparse{\mydepth}: \pgfmathresult,}

\hbox{\edef\mydepth{\dimexpr0.2in+0.2cm}:\pgfmathparse{\mydepth}: \pgfmathresult}

\end{document}

Which produces:

:: 20.14423,
:: 20.14423

This is a bug in PGFmath but has now been fixed in the PGF CVS version.

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.