0

I have a massive block of code with multiple figures in it. I am looking for an easy way to draw lines anywhere within the massive figure. This is the block of code that I have.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{float}
\usepackage{subfig}
\usepackage{tikz}
\title{test}
\author{test}%
\date{January 2022}
\usepackage{graphicx}
\begin{document}

\begin{figure*}
\centering
\subfloat{
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
\end{tikzpicture}}\hspace{-0.2em}
\subfloat{
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
\end{tikzpicture}}\hspace{-0.2em}
\subfloat{
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
\end{tikzpicture}}\hspace{-0.2em}
\subfloat{
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
\end{tikzpicture}}\hspace{-0.2em}
\subfloat{
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
\end{tikzpicture}}\hspace{-0.2em}
\subfloat{
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
\end{tikzpicture}}

\subfloat{
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
\end{tikzpicture}}\hspace{-0.2em}
\subfloat{
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
\end{tikzpicture}}\hspace{-0.2em}
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[inner sep=0] (image) at (0,0) {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
\end{tikzpicture}}\hspace{-0.2em}
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
\end{tikzpicture}}\hspace{-0.2em}
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
\end{tikzpicture}}\hspace{-0.2em}
\subfloat{
\begin{tikzpicture}[remember picture]
    \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
\end{tikzpicture}}

\noindent\begin{tabular}{@{}rl}     
    True Left &\includegraphics[width=0.85\textwidth,height=0.02\textwidth,trim={0 0 0 0},clip]{example-image-a}\\
    Pred Left &\includegraphics[width=0.85\textwidth,height=0.02\textwidth,trim={0 0 0 0},clip]{example-image-a}\\
    True Right &\includegraphics[width=0.85\textwidth,height=0.02\textwidth,trim={0 0 0 0},clip]{example-image-a}\\
    Pred Right &\includegraphics[width=0.85\textwidth,height=0.02\textwidth,trim={0 0 0 0},clip]{example-image-a}\\
\end{tabular}
\caption{}
\label{fig:qualitative-mocap}
\end{figure*}

\end{document}

And this is the figure I would like with several example lines drawn over it.

enter image description here

I received a solution at How to use tikz to draw lines over multiple figures? How to declare tikzpicture over multiple tikzpictures? but it feels like a solution that does not generalize well to messy figure environments with subfloats and tables inside. I have spent a couple of hours still trying to figure out how to extend that to my problem.

Please is there a simple way to assign the coordinate [0,0] to the top left and [1,1] to the bottom right of the entire figure? And then I simply draw over as if it is one big figure? Here would be an example line that obviously does not make any sense.

\draw [-stealth, thick, \ref{fig:qualitative}] (0.75,0.9) -- (0.75,0.6);

The lines in the figure would look something like this

enter image description here

0

1 Answer 1

1

Rather than create a lot of tikzpictures separated by \hfill, this uses the calc tikzlibrary to achieve the same effect.

Note that [remember picture] uses the aux file, so it takes two runs to correctly position the lines.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{calc}
%\usepackage{float}% not used
%\usepackage{subfig}
%\title{test}
%\author{test}%
%\date{January 2022}
%\usepackage{graphicx}% redundant with tikz
\usepackage{showframe}% MWE only

\begin{document}

\begin{figure}
\centering
\sbox9{\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image}}% compute image height
\begin{tikzpicture}[remember picture]
    \begin{scope}[local bounding box=bottom row]
      \path (0,0) (\textwidth-\wd9, 0);
    \end{scope}
    \begin{scope}[yshift={\ht9+\dp\strutbox}, local bounding box=top row]% add gap between rows
      \path (0,0) (\textwidth-\wd9, 0);
    \end{scope}
    \node[inner sep=0pt] (A1)  at (top row.west)
      {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
    \node[inner sep=0pt] (A2)  at ($(top row.west)!0.2!(top row.east)$)
      {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
    \node[inner sep=0pt] (A3)  at ($(top row.west)!0.4!(top row.east)$)
      {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
    \node[inner sep=0pt] (A4)  at ($(top row.west)!0.6!(top row.east)$)
      {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
    \node[inner sep=0pt] (A4)  at ($(top row.west)!0.8!(top row.east)$)
      {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
    \node[inner sep=0pt] (A6)  at (top row.east)
      {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
      
    \node[inner sep=0pt] (B1)  at (bottom row.west)
      {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
    \node[inner sep=0pt] (B2)  at ($(bottom row.west)!0.2!(bottom row.east)$)
      {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
    \node[inner sep=0pt] (B3)  at ($(bottom row.west)!0.4!(bottom row.east)$)
      {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
    \node[inner sep=0pt] (B4)  at ($(bottom row.west)!0.6!(bottom row.east)$)
      {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
    \node[inner sep=0pt] (B4)  at ($(bottom row.west)!0.8!(bottom row.east)$)
      {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
    \node[inner sep=0pt] (B6)  at (bottom row.east)
      {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
\end{tikzpicture}

\noindent\begin{tabular}{@{}r@{\hspace{\gap}}l@{}}     
\makebox[\wd9][r]{True Left} &\includegraphics[width=0.83\textwidth,height=0.02\textwidth,trim={0 0 0 0},clip]{example-image-a}\\
    Pred Left &\includegraphics[width=0.83\textwidth,height=0.02\textwidth,trim={0 0 0 0},clip]{example-image-a}\\
    True Right &\includegraphics[width=0.83\textwidth,height=0.02\textwidth,trim={0 0 0 0},clip]{example-image-a}\\
    Pred Right &\includegraphics[width=0.83\textwidth,height=0.02\textwidth,trim={0 0 0 0},clip]{example-image-a}\\
\end{tabular}

\begin{tikzpicture}[remember picture]
    \node (C1) {Label here};
    \begin{pgfinterruptboundingbox}% same as [overlay]
      \draw[thick] ($(B1.center)!0.5!(B1.south)$) -- (C1.west);
    \end{pgfinterruptboundingbox}
\end{tikzpicture}
\caption{}
\label{fig:qualitative-mocap}
\end{figure}

\end{document}

demo


Alternatively, you can precompute the needed gap size and use [right=<gap>] to align the nodes.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{calc}
%\usepackage{float}% not used
%\usepackage{subfig}
%\title{test}
%\author{test}%
%\date{January 2022}
%\usepackage{graphicx}% redundant with tikz

\usepackage{showframe}% MWE only

\newlength{\gap}

\begin{document}

\begin{figure}
\centering
\sbox9{\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image}}% compute image size
\setlength{\gap}{\dimexpr 0.2\textwidth-1.2\wd9-0.2pt}% 6/5 = 1.2, fudge factor for round off error
\begin{tikzpicture}[remember picture]
    \node[inner sep=0pt] (A1) at (0,0)
      {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
    \node[inner sep=0pt, right=\gap] (A2) at (A1.east)
      {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
    \node[inner sep=0pt, right=\gap] (A3) at (A2.east)
      {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
    \node[inner sep=0pt, right=\gap] (A4) at (A3.east)
      {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
    \node[inner sep=0pt, right=\gap] (A5) at (A4.east)
      {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
    \node[inner sep=0pt, right=\gap] (A6) at (A5.east)
      {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
      
    \node[inner sep=0pt, below=\gap] (B1) at (A1.south)
      {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
    \node[inner sep=0pt, below=\gap] (B2) at (A2.south)
      {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
    \node[inner sep=0pt, below=\gap] (B3) at (A3.south)
      {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
    \node[inner sep=0pt, below=\gap] (B4) at (A4.south)
      {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
    \node[inner sep=0pt, below=\gap] (B4) at (A5.south)
      {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
    \node[inner sep=0pt, below=\gap] (B6) at (A6.south)
      {\includegraphics[width=0.15\textwidth,trim={0 0 0 0},clip]{example-image-a}};
\end{tikzpicture}

\setlength{\dimen9}{\dimexpr 5\wd9+4\gap}%
\makebox[\wd9][r]{True Left}\hskip\gap \includegraphics[width=\dimen9,height=0.02\textwidth,trim={0 0 0 0},clip]{example-image-a}
\makebox[\wd9][r]{Pred Left}\hskip\gap \includegraphics[width=\dimen9,height=0.02\textwidth,trim={0 0 0 0},clip]{example-image-a}
\makebox[\wd9][r]{True Right}\hskip\gap \includegraphics[width=\dimen9,height=0.02\textwidth,trim={0 0 0 0},clip]{example-image-a}
\makebox[\wd9][r]{Pred Right}\hskip\gap \includegraphics[width=\dimen9,height=0.02\textwidth,trim={0 0 0 0},clip]{example-image-a}

\begin{tikzpicture}[remember picture]
    \node (C1) {Label here};
    \begin{pgfinterruptboundingbox}% same as [overlay]
      \draw[thick] ($(B1.center)!0.5!(B1.south)$) -- (C1.west);
    \end{pgfinterruptboundingbox}
\end{tikzpicture}
\caption{}
\label{fig:qualitative-mocap}
\end{figure}

\end{document}
4
  • Thank you very much. It is amazing. But I was wondering why the caption "Figure 1:" got pushed below. And what does \draw[thick] ($(B1.center)!0.5!(B1.south)$) mean? I went through bu.edu/math/files/2013/08/tikzpgfmanual.pdf and I do not see that syntax at all for drawing lines
    – Kong
    Aug 12, 2022 at 18:05
  • I went through documentation.libreoffice.org/assets/Uploads/Documentation/en/… and I still do not know what the exclamation mark means and what ($(B1.center)!0.5!(B1.south)$) means. Dear god latex is so fucking cancer.
    – Kong
    Aug 12, 2022 at 18:21
  • The calc noation is found in section 13.5 (page 148) of the PGF manual. There are several variations, but ($(A)!0.5!(B)$) means half way between (A) and (B). Aug 13, 2022 at 13:06
  • Your link is for version 1.18. The current version is 3.1.9a, which you can down load from ctan.org/pkg/pgf?lang=en Aug 13, 2022 at 13:13

You must log in to answer this question.

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