3

The following question has essentially already been asked on this website (not by me), however none of the answers are satisfactory for my purposes, so I'll ask it again in a way that will hopefully elicit helpful answers.

Consider the following LaTeX document containing two sentences.

\documentclass{article}
\usepackage{lipsum}

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

\begin{document}

\lipsum[1][1]\lipsum[1][2]

\end{document}

When this code is compiled with pdflatex, the following output is produced.

A simple text document without a TikZ picture

I would like to position a 1cm wide TikZ square 1cm below and 1cm to the right of the bottom left corner of the capital U letter, as illustrated below. (I've indicated the reference point with a red dot, but this red dot is not supposed to appear in the actual output.)

The desired output

How can I achieve this?

As a first attempt, I tried the following code.

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

\newcommand{\mytikzrec}{%
\begin{tikzpicture}[baseline=2cm]%
\draw (0,0) rectangle (1,1);%
\end{tikzpicture}%
}

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

\begin{document}

\lipsum[1][1]\mytikzrec\lipsum[1][2]

\end{document}

This produced the following output.

First attempt

This has two problems. Firstly, I managed to move the square down, but I didn't know how to move the square to the right. Secondly, the normal text flow was disrupted, which is not what I desire. The text should flow as though the picture didn't exist, just like in the case of the first code sample above.

3 Answers 3

6

You can use the tikzmark TikZ library for this:

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

\begin{document}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. \tikzmarknode{U}{U}t purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis.

\begin{tikzpicture}[remember picture,overlay]
\draw (U.south west) ++ (1,-1) rectangle ++ (1,-1);
% just for showing the distances
\draw[red,-latex] (U.south west) |- ++ (1,-1);
\end{tikzpicture}
\end{document}

enter image description here

3
  • Thank you! Is there a way to achieve a similar output without encapsulating the letter U in a node? By placing an invisible or dummy node between the period and the U?
    – Evan Aad
    Dec 14, 2021 at 8:23
  • 1
    @EvanAad, yes, just change \tikzmarknode{U}{U}t for \tikzmarknode{U}{}Ut and you have it. Dec 14, 2021 at 8:37
  • 2
    Or just \tikzmark{U} before the text. Then use (pic cs:U) for the coordinate in the subsequent tikzpicture. Dec 14, 2021 at 10:47
5

I'd like to suggest a variation on Juan Castaño's answer, which uses \tikzmark instead of \tikzmarknode, which yields a more succinct 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) ++ (1,-1) rectangle ++(1,-1);%
\end{tikzpicture}
\end{document}
2
  • 1
    Didn't see this when I left my comment on Juan's answer - yes, this is what I'd use. Dec 14, 2021 at 10:49
  • @AndrewStacey, yep, both of you are right, this is simpler. Dec 14, 2021 at 11:18
4

Although Juan Castaño answer is more general (learning to use \tikzmark is really useful), the simpler answer is --- hide the size of your TikZ picture with overlay, so that it will be considered a 0-size thing by LaTeX:

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

\newcommand{\mytikzrec}{%
\begin{tikzpicture}[overlay]%
\draw (1,-1) rectangle ++(1,-1);%
\end{tikzpicture}%
}

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

\begin{document}

\lipsum[1][1]\mytikzrec\lipsum[1][2]

\end{document}

enter image description here

(Notice that the units used by TikZ by default are 1cm in both axes, although you can tweaks that in several ways).

Using \tikzmark is much more powerful but often it will require several compilations to get things in place.

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.