3

This is a follow-up to this question. I'd like to achieve the same output as described in the question, namely a 1cm wide square that is 1cm below and 1cm to the right of the red dot in the following illustration (the dot should not appear in the output):

Desired output

Taking a lead from this answer, I've attempted to use the tikzmark package, but unlike the answer, I tried to use the \tikzmark command rather than \tikzmarknode. I based my attempt on the first example listed in the tikzmark package documentation on p. 10, namely on the following code:

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

\title{}
\author{}
\date{}

\begin{document}
\[
\tikzmark{a} e^{i \pi/2} = i
\]
This\tikz[remember picture,overlay,baseline=0pt] \draw[->] (0,1em)
to[bend left] ([shift={(-1ex,1ex)}]pic cs:a); is an important
equation.
\end{document}

which produces the following output (after compiling twice): The output of the 1st example given in the TikZ manual

This output conforms to my expectations and to the drawing shown in the tikzmark documentation.

Based on this example, I tried solving the original problem using the following code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usepackage{lipsum}

\title{}
\author{}
\date{}

\begin{document}

\lipsum[1][1]\tikzmark{x}\lipsum[1][2]

\begin{tikzpicture}[remember picture,overlay]%
\draw (pic cs:x) ++ (1cm,-1cm) rectangle (1cm,-1cm);%
\end{tikzpicture}
\end{document}

This produced the following output (after compiling twice):

My attempt

which is not what I expected for two reasons. Firstly, the shape is a rectangle rather than a square. Secondly, it is positioned to the left of the mark rather than to its right.

Why doesn't my code work as expected? How can it be fixed?

8
  • Try to use --++ instead of just ++. And did you compile twice?
    – SebGlav
    Dec 14, 2021 at 9:51
  • @SebGlav: Yes, I did compile twice. Replacing ++ with --++ only adds a line from the mark to the rectangle's top right corner.
    – Evan Aad
    Dec 14, 2021 at 10:05
  • 2
    The rectangle is drawn from the point located (1cm,-1cm) from the (pic cs:x) coordinate to a point which is at the absolute (for this TikZ picture) (1cm, -1cm). Maybe you want relative coordinate on the second corner?
    – Rmano
    Dec 14, 2021 at 10:16
  • 2
    @EvanAad as shown in the answer by @sebglav, just use ++ on that coordinate too.
    – Rmano
    Dec 14, 2021 at 10:22
  • 1
    @EvanAad --- it's stated in the last two lines of the existing answer, no need to write another one!
    – Rmano
    Dec 14, 2021 at 10:47

1 Answer 1

5

You need to draw carefully your square by using rectangle++ so that your second corner is drawn from the first one. And here, I used the calc library to position exactly the first corner of your square, but other options exist.

tikz relative to tikzmark

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc}
\usepackage{lipsum}

\title{}
\author{}
\date{}

\begin{document}

\lipsum[1][1]\tikzmark{x}{}\lipsum[1][2]

\begin{tikzpicture}[remember picture,overlay]%
    \filldraw[red,opacity=0.5] (pic cs:x) circle(2pt);
    \draw ($(pic cs:x) + (1,-1)$) rectangle ++ (1,-1);%
\end{tikzpicture}
\end{document}

Without using calc library, you can adjust your initial command like this:

\draw (pic cs:x) ++ (1,-1) rectangle ++(1,-1);

which produces the exact same output.

You must log in to answer this question.

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