3

I just tried to use the relative positioning of nodes with respect to the current bounding box of a tikzpicture as proposed in Position node relative to current bounding box.

The proposed solution works fine (unsurprisingly of course) for the requested case. However, when trying to draw a path containing to[out=...,in=...] statements, the compiler fails with

Package tikz Error: (, +, coordinate, pic, or node expected

As proposed in the Log, I

Pretend that you're Hercule Poirot: Examine all clues, and deduce the truth by order and method.

but up to now, nothing came out... If you have any ideas, thank you in advance.

Here is the MWE

\documentclass[tikz,border=1mm]{standalone}
\usetikzlibrary{calc}

% Relative positioning in tikzpicture as per https://tex.stackexchange.com/questions/274893/position-node-relative-to-current-bounding-box
\newcommand\relcoord[2]{({$(current bounding box.south west)!#1!(current bounding box.south east)$} |- {$(current bounding box.south west)!#2!(current bounding box.north west)$})}

\begin{document}
    \begin{tikzpicture}
        % Bounding box setup
        \draw[black] (0,0) rectangle (10,10);
        \useasboundingbox \relcoord{0}{0} rectangle \relcoord{1}{1};
        % Red rectangle -> OK
        \draw[red] \relcoord{0.1}{0.1} rectangle \relcoord{0.8}{0.8};
        % Blude dashed lines -> OK
        \draw[blue,dashed] \relcoord{0.1}{0.1} -- \relcoord{0.1}{0.8} -- \relcoord{0.8}{0.8} -- \relcoord{0.8}{0.1} -- cycle;
        % Orange curves -> KO, to be commented for compilation
        \draw[orange,dashed] \relcoord{0.1}{0.1} to[out=90,in=-90] \relcoord{0.1}{0.8} to[out=0,in=180] \relcoord{0.8}{0.8} to[out=-90,in=90] \relcoord{0.8}{0.1} -- cycle;
    \end{tikzpicture}
\end{document}
1
  • 2
    While someone solves the question you can obey compiler and insert a + which implies to change relative values: \draw[orange,dashed] \relcoord{0.1}{0.1} to[out=90,in=-90] +\relcoord{0.0}{0.7} to[out=0,in=180]+\relcoord{0.7}{0} to[out=-90,in=90] +\relcoord{0.0}{-0.7} -- cycle;
    – Ignasi
    Aug 9, 2018 at 12:15

1 Answer 1

5

I don't think you need the \relcoord command at al if you fix the bounding box with \useasboundingbox.

You can define a style

\tikzset{
    use bounding box relative coordinates/.style={
        shift={(current bounding box.south west)},
        x={(current bounding box.south east)},
        y={(current bounding box.north west)}
    },
}

That ensures that (0,0) is at south west, (1,0) is at south east, (0,1) is at north west, and consequently, (1,1) is at north east of the current bounding box.

You can call this style in a scope with

\begin{scope}[use bounding box relative coordinates]
    <your commands>
\end{scope}

This way you don't even need the calc library to get this:

enter image description here

Full MWE:

\documentclass[tikz,border=1mm]{standalone}

\tikzset{
    use bounding box relative coordinates/.style={
        shift={(current bounding box.south west)},
        x={(current bounding box.south east)},
        y={(current bounding box.north west)}
    },
}

\begin{document}
    \begin{tikzpicture}
        % Bounding box setup
        \draw[black,use as bounding box] (-5,-5) rectangle (5,5);
        \begin{scope}[use bounding box relative coordinates]
            % Red rectangle -> OK
            \draw[red] (0.1,0.1) rectangle (0.8,0.8);
            % Blude dashed lines -> OK
            \draw[blue,dashed] (0.1,0.1) -- (0.1,0.8) -- (0.8,0.8) -- (0.8,0.1) -- cycle;
            % Orange curves -> OK
            \draw[orange,dashed] (0.1,0.1) to[out=90,in=-90] (0.1,0.8) to[out=0,in=180] (0.8,0.8) to[out=-90,in=90] (0.8,0.1) -- cycle;
        \end{scope}
    \end{tikzpicture}
\end{document}

Small remark on using to commands in this relative scope:

Imagine that you use \draw[orange,dashed] (0.1,0.5) to[out=90,in=270] (0.8,0.5); to draw a line with the perfect shape, depending on the rectangle you use as bounding box.

enter image description here

But now you would rather have a rectangle scaled only in y direction, so the wave form must scale with it. Only it does not.

enter image description here

At this point you might decide that is better to use the .. controls (<handle1>) and (<handle2>) .. drawing style, as follows: \draw[green,line width=1pt,dashed] (0.1,0.5) .. controls ++(0,0.27) and ++(0,-0.27) .. (0.8,0.5);.

enter image description here

11
  • Thanks, that's a different answer than excepted, but really nice though !
    – BambOo
    Aug 9, 2018 at 13:24
  • @BambOo is it useful for you in this form?
    – Max
    Aug 9, 2018 at 20:20
  • 1
    Yes, totally relevant for my application. It is really nice to be able to use the standard (x,y) syntax for relative postions
    – BambOo
    Aug 9, 2018 at 22:17
  • 1
    @BambOo great! Good to hear.
    – Max
    Aug 10, 2018 at 14:00
  • 1
    I can't seem to replicate this. Maybe you could ask a new question with MWE?
    – Max
    Aug 11, 2018 at 14:04

You must log in to answer this question.

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