1

I am generating some complex plots programmatically. They are exported into PDF and use native PDF features.

However, I would like to (1) include them in a paper written in LaTeX, and (2) use LaTeX math for some labels.

I had a vague recollection of a program called xfig that allowed a combination PDF+TeX plot. I looked at how it does it, and apparently it uses the a zero-size picture environment with \includegraphics, and then another picture for the text as in eg

\documentclass{standalone}
\usepackage{graphicx,xcolor}
\begin{document}
\begin{picture}(0,0)%
\includegraphics{example-image.pdf}%
\end{picture}%
\setlength{\unitlength}{4144sp}%
\begin{picture}(5604,4254)(2284,-5698)
\put(3916,-3166){\makebox(0,0)[lb]{\smash{\fontsize{12}{14.4}\normalfont {\color[rgb]{0,0,0}$\int_0^1 f(x)dx$}%
}}}
\end{picture}%
\end{document}

All I need is (a) loading the PDF as the background, and (b) a mechanism to place boxes with LaTeX at given coordinates. Is this the current recommended solution?

(I am focusing on speed. Tried pgf but complex figures slow it down considerably. The LaTeX documents are compiled to PDF using pdflatex.)

7
  • 2
    Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for the users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. Commented Nov 21 at 9:00
  • I added those, but since there is no included figure it attached it will not compile anyway. In any case this is more or a conceptual question. Commented Nov 21 at 9:39
  • 2
    You could use something like example-image or example-image-duck instead. That's included in all full tex installations. Commented Nov 21 at 9:42
  • Thanks! I did not know about that Commented Nov 21 at 9:43
  • 1
    Good find : ) ... All Tikz-solutions implicitely use what I showed below in tex.stackexchange.com/a/755151/245790 . You can also combine most of these, e.g. with the elementary ways I showed: they are built on Tikz, so you can use Tikz as well. Commented Nov 21 at 11:01

2 Answers 2

3

You can use overpic:

\documentclass{article}
\usepackage{graphicx,xcolor}
\usepackage{overpic}

\begin{document}
\begin{overpic}[percent,width=6cm]{example-image.pdf}
  \put(60,10){%
    \makebox(0,0)[lb]{%
      \fontsize{12}{14.4}\normalfont
      \color{red!80}$\displaystyle\int_0^1 f(x)\,dx$%
    }%
  }%
\end{overpic}

\end{document}

output

1
  • Thanks! This looks like the solution I am looking for. Is there a way to align text vertically/horizontally? Eg to the left/right of the point, above or below, on baseline, etc. Commented Nov 24 at 8:08
1

Here is a way to do it with Tikz:

result

Tikz primer

Within LaTeX you can insert a tikzpicture environment.

Within it you can place \nodes: consider them as boxes or labels you can place with (almost) any LaTeX content.

A \node can have:

  • options in [ ]
  • a name, like (A)
  • a specified position
  • LaTeX content inside { }
  • end such statements by ;

The most important one for you will just include your image, like so: \node (A) {\includegraphics{example-image}};

Several ways to place nodes

Node (A), the image is placed without a coordinate. So A's center by default is at (0,0).

Node (a) in red is also placed implicitely at the default (0,0), and is hard to read from the images label.

Node (b) in teal is placed top left (north west) of A, AND its own reference point is moved from the default ccnter to top left (north west), so it's displayed inside the image. The option to know is anchor.

Node (c) in orange is similar but placed at the intersection of the outer shape with a 110 deg beam from the center. To better see this I also drew the nodes shape in brown. The overlap to the outside is (probably) caused by the non-zero inner sep otpion. You also see, how to manipulate font size, if needed. // The \draw statement just shows the situation.

You also could use xshift and yshift, if that's more convenient, instead of guessing an angle.

Node (d) in magenta shows some absolute placement. While the variants above are usedul, if you don't know or don't care about the images dimensions, here you certaibly need to know them.

Node (e) in blue is ismilar, but uses polar coordinates to place this nodes center. (Often it's more convenient to place west, north west etc. of the node: use anchor in this situation.)

Finally, if you named relevant nodes, it's simple to add e.g. text as needed, here relative to node (d).

For tutorials and all options kindly check the Tikz manual, e.g. in this online version.

Code

\documentclass[10pt,border=3.1mm,tikz]{standalone}  % adjust border
\usepackage{graphicx}

\begin{document}
  \begin{tikzpicture}[  
  ]
    % ~~~ showing the image ~~~~~~~~~~~~~~~~~~~~~~~~
    \node (A) {\includegraphics{example-image}};    % looks for .pdf
    
    % ~~~ various ways to put math ~~~~~~~~~
    \node[red] (a) {$\int_0^1 f(x)dx$};             % at default (0,0)
    
    \node[teal,anchor=north west] (b) 
                                at (A.north west) 
                                {$\int_0^1 f(x)dx$};% at top left
    
    \node[orange,draw=brown,                        % shows shape
          anchor=north west,
          font=\huge]           (c) 
                                at (A.110) 
                                {$\int_0^1 f(x)dx$};% at top left
    \draw [draw=orange] (0,0) -- (A.110);           % showing 110 deg
    
    \node[magenta] (d) at (-3,-2) {$\int_0^1 f(x)dx$};  % at default (-3,-2)
    \node[blue   ] (e) at (-30:4) {$\int_0^1 f(x)dx$};  % polar coordinates
    
    % ~~~  if you need to do more, e.g. ~~~~~~~~~~~~~
    \node[anchor=north] at (d.south) {this shows, that \dots};

  \end{tikzpicture}
\end{document}

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.