使用宏与长度设置TikZ坐标会影响tikzpicture的放置

使用宏与长度设置TikZ坐标会影响tikzpicture的放置

我有点困惑为什么这两个 MWE 的结果会导致不同的输出。

示例 1(期望输出)

在此示例中,我使用长度设置 y 坐标值:

\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}

在此处输入图片描述

示例 2(输出不理想)

在此示例中,我使用宏设置 y 坐标值:

\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}

在此处输入图片描述

实际上,对于第二个示例,我不需要宏。最初,此问题出现在以下代码中:

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

所以问题其实不是对比长度。这是计算坐标中长度的问题。

答案1

使用

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

我收到语法错误

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

并且没有输出。

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

我得到与以下相同的输出

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

添加\listfiles到您的第二个示例中,我收到一个错误,然后列表显示如下。您可以将文件版本与您的设置进行比较:

\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

%“icky” 输出是由于解析器代码缺失而插入空格的结果pgfmath。这可以从以下代码中看到:

\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}

其结果为:

:: 20.14423,
: : 20.14423

有问题的命令是,在开头\pgfmath@parse@dimexpr@缺少:%{

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

通过添加,%空间消失了:

\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}

其结果为:

:: 20.14423,
:: 20.14423

这是一个错误,PGFmath但现在已在 PGF CVS 版本中修复。

相关内容