Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'd like to scale a line according to \figscale. Here it is said that the below syntax should work in general, but in this graphic it does not.

As the picture shows, the numbers are just concatenated to, e.g., -0.53cm instead of -0.5 * 3cm = 1.5cm, although I'd like to multiply them. Writing -0.5*\figscaleworks well.

Why is the solution provided in the link not applicable here?

\def \figscale{3cm}

\begin{figure}
    \centering
    \begin{tikzpicture}
        \draw (-1.0,2) -- (0,2) node[midway, anchor=south] {$A$};
        \draw (-0.5\figscale,1) -- (0,1) node[midway, anchor=south] {$B$};
        \draw (-1.0\figscale,0) -- (0,0) node[midway, anchor=south] {$C$};
    \end{tikzpicture}
\end{figure}

enter image description here

share|improve this question
2  
\def \figscale{3cm} defines a macro that is simply replaced by 3cm where used, it's not a length. Try \newlength\figscale \setlength\figscale{3cm}. –  cgnieder Jun 25 at 10:07

2 Answers 2

up vote 4 down vote accepted

In the answers to the linked question, \textwidth is used, which is a length parameter for TeX, so the syntax rules allow 4\textwidth (or any decimal number instead of 4).

When TeX is looking for a length, it expands tokens as it goes. Since it expects a length and it has seen -0.5 it expands the next token which might result in something that continues the decimal digits or something that begins with a length register. Your case is the former, because \figscale expands to 3cm and

-0.53cm

is a perfect length specification for TeX. The situation is a bit more complicated because we're in the middle of a coordinate for TikZ and this explains why -0.5*\figscale works.

If you want a macro that behaves like a dimension, use

\newcommand{\figscale}{\dimexpr3cm\relax}

because TeX translates \dimexpr3cm\relax into a (nameless) internal length register. Nonetheless, you're probably best served with

\newlength{\figscale}
\setlength{\figscale}{3cm}
share|improve this answer

Prefixing a "variable" with a number, for example, 4\textwidth only works if the variable is a TeX dimension or skip or box dimension. This is consistent with usual TeX behaviour.

As the latest PGF manual states (section 88.1 "Commands for Parsing Expressions")

The parser will recognize TEX registers and box dimensions, so \mydimen, 0.5\mydimen, \wd\mybox, 0.5\dp\mybox, \mycount\mydimen and so on can be parsed

The math parser expands every expression with \edef before parsing, so the MWE given above, -0.5\figscale becomes -0.53cm which is evaluated as -15.07993 (points) as opposed to the desired -42.67912.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.