# Saving an image containing labels produced using LaTeX code

For a paper I'm working on, I have a bunch of figures (pdf files) generated using Mathematica which I label using \put commands in LaTeX. However, I want to now save the labeled figures as pdf files.

Another way to label them would be to open each pdf file in an image editor such as Inkscape or GIMP and then manually add the LaTeX symbols using LaTeXit, but I really want to avoid doing this. Yet another way would to be to graft the labels in my Mathematica code, but again I want to not do this.

Is there a way that a pdf image additionally embellished with LaTeX code can be saved as a new pdf image?

• standalone package for example will make it easy to make a 1-page pdf sized to the image (+ latex text) which you can then use elsewhere – David Carlisle Sep 9 '18 at 13:32
• Thanks for your reply. Will the image become a full page? I just want the image to be of the same dimensions as the original image (the labeling is inside the image). – leastaction Sep 9 '18 at 13:51
• yes but the page will be sized to the image, so it does what you want:-) – David Carlisle Sep 9 '18 at 14:00
• The page is not sized to the image -- the output by default produces a full sized page with page number. I was hoping to get a page sized to the image (as a pdf file), which I could then graft into other LaTeX documents. – leastaction Sep 25 '18 at 15:11
• no see the abstract of the standalone class doc (texdoc standalone) its main purpose is to make output files cropped to the content. – David Carlisle Sep 25 '18 at 15:19

Another way of obtaining from unlabeled images labeled images as extra pdf-files, which in turn can be included in your document via \includegraphics, is maintaining your labeled images as separate LaTeX documents, consisting of one page whose measurements correspond to the measurements of the underlying image scaled to that size in which the image shall appear within the document.

With the following template, \includegraphics is carried out within a box.

That box is used both for determining the measurements of the pages of the document, and for having the image as background-image.

After doing that, I could use my following template for creating a labelled pdf-file—I compiled using pdflatex:

%------------------------------------------------------
% Load whatever documentclass you need:
%------------------------------------------------------
\documentclass{article}
%------------------------------------------------------
% These packages are needed for detecting the engine:
%------------------------------------------------------
\usepackage{iftex, ifpdf}
%------------------------------------------------------
% These packages are needed for placing the image:
%------------------------------------------------------
\usepackage{graphicx}
\usepackage{eso-pic}
%------------------------------------------------------
% the look of your texts:
%------------------------------------------------------
\usepackage{color}
% \usepackage...
% ...
%------------------------------------------------------
% Define whatever macros you need:
%------------------------------------------------------
% ...
%------------------------------------------------------
% Put your image into a box so you can use \wd, \ht and
% \dp for measuring that box and setting the margins
% and the paper-format accordingly:
%
% ( In case of dvi-mode and an image not in .eps-format,
% you may need to calculate the bounding-box, e.g., via
% the program ebb, and then add a Graphics-Rule,
% something like this:
%
% \DeclareGraphicsRule{.jpg}{eps}{.bb}{}
%
% It depends on your program for viewing dvi-files
% whether it can show non-eps-images. )
%
\newbox\BoxWithImage
\setbox\BoxWithImage=\vbox{%
\hbox{%
%-------------------------------------------------
%-------------------------------------------------
\includegraphics[width=14.5cm]{Elephant.jpg}%
}%
}%
%------------------------------------------------------
% A macro for expressing lengths in terms of
% \unitlength - useful with the picture-environment:
%------------------------------------------------------
\makeatletter
\newcommand\ConvertToUnitlength[1]{%
\strip@pt\dimexpr#1*65536/\number\dimexpr\unitlength\relax\relax
}%
\makeatother
%------------------------------------------------------
% Adjust the paper-format and the margins:
%
%   Hint in case you don't know how to do this:
%
%   The package layout (<https://ctan.org/pkg/layout>)
%   provides information about the different lengths that
%   play a role in the Layout of a LaTeX document.
%   It defines the command \layout which produces a
%   visible overview of the layout of the current document.
%
%   Additionally to the info provided by that package,
%   with pdfTeX-based engines you also need to know
%
%------------------------------------------------------
\setlength\paperwidth{\the\wd\BoxWithImage}
\setlength\paperheight{\the\ht\BoxWithImage}
\makeatletter
\ifXeTeX
\pdfpagewidth=\paperwidth
\pdfpageheight=\paperheight
\else
\ifLuaTeX
\pagewidth=\paperwidth
\pageheight=\paperheight
\else
\ifPDFTeX
\pdfpagewidth=\paperwidth
\pdfpageheight=\paperheight
\fi
\fi
\ifpdf\else
\GenericWarning{(hint) }{(hint)
--------------------------------------------------------------\MessageBreak
Your TeX-engine seems to be running in dvi-mode.\MessageBreak
In case your image is not an .eps-file, you may need to do\MessageBreak
the following:\MessageBreak
1. Have calculated the bounding box of your image and saved\MessageBreak
\space\space\space the bounding-box-info in a file with extension .bb or .xbb\MessageBreak
\space\space\space by means of a program like ebb (=extract bounding box)\MessageBreak
2. Declare a new graphics-rule, which could, e.g., look like\MessageBreak
\space\space\space this:\MessageBreak
\string\DeclareGraphicsRule{.jpg}{eps}{.bb}{}\MessageBreak
--------------------------------------------------------------\@gobbletwo%
}%
\fi
\fi
\makeatother
\setlength\textwidth{\paperwidth}
\setlength\textheight{\paperheight}
\setlength\evensidemargin{0cm}
\setlength\oddsidemargin{\evensidemargin}
\setlength\marginparwidth{0ex}
\setlength\marginparsep{0ex}
\setlength\topmargin{0cm}
\setlength\footskip{0cm}
\setlength\topskip{0ex}
%------------------------------------------------------
%------------------------------------------------------
\pagestyle{empty}
%------------------------------------------------------
%------------------------------------------------------
\parindent=0ex
\parskip=\baselineskip
%------------------------------------------------------
% Make your image the background of all pages:
%------------------------------------------------------
%------------------------------------------------------
% Let's write the document. Hereby make sure to have
% only one page.
%------------------------------------------------------
\begin{document}
% Put things into a \vbox in order to get overfull-box-
% warnings rather than several pages:
\vbox to \paperheight{%
%----------------------------------------------------
%       Within the \vbox do whatever you like:
%----------------------------------------------------
\leavevmode\color{red}\Huge
XX\hfill XX
\vfill
\hfill The center.\hfill\null
\vfill
XX\hfill XX
%----------------------------------------------------
}%
\end{document}


I obtained the following pdf-file:

Now I could use this within my documents via \includegraphics.

I think, all you need to do for adjusting the template is adjusting the \includegraphics-command so you get the right background-image with the right measurements, and adjusting the content of the \vbox to \paperheight.

E.g., changing the content of the \vbox to \paperheight so that it holds a picture-environment, where I could use \put, yielded:

%------------------------------------------------------
% Load whatever documentclass you need:
%------------------------------------------------------
\documentclass{article}
%------------------------------------------------------
% These packages are needed for detecting the engine:
%------------------------------------------------------
\usepackage{iftex, ifpdf}
%------------------------------------------------------
% These packages are needed for placing the image:
%------------------------------------------------------
\usepackage{graphicx}
\usepackage{eso-pic}
%------------------------------------------------------
% the look of your texts:
%------------------------------------------------------
\usepackage{color}
% \usepackage...
% ...
%------------------------------------------------------
% Define whatever macros you need:
%------------------------------------------------------
% ...
%------------------------------------------------------
% Put your image into a box so you can use \wd, \ht and
% \dp for measuring that box and setting the margins
% and the paper-format accordingly:
%
% ( In case of dvi-mode and an image not in .eps-format,
% you may need to calculate the bounding-box, e.g., via
% the program ebb, and then add a Graphics-Rule,
% something like this:
%
% \DeclareGraphicsRule{.jpg}{eps}{.bb}{}
%
% It depends on your program for viewing dvi-files
% whether it can show non-eps-images. )
%
\newbox\BoxWithImage
\setbox\BoxWithImage=\vbox{%
\hbox{%
%-------------------------------------------------
%-------------------------------------------------
\includegraphics[width=14.5cm]{Elephant.jpg}%
}%
}%
%------------------------------------------------------
% A macro for expressing lengths in terms of
% \unitlength - useful with the picture-environment:
%------------------------------------------------------
\makeatletter
\newcommand\ConvertToUnitlength[1]{%
\strip@pt\dimexpr#1*65536/\number\dimexpr\unitlength\relax\relax
}%
\makeatother
%------------------------------------------------------
% Adjust the paper-format and the margins:
%
%   Hint in case you don't know how to do this:
%
%   The package layout (<https://ctan.org/pkg/layout>)
%   provides information about the different lengths that
%   play a role in the Layout of a LaTeX document.
%   It defines the command \layout which produces a
%   visible overview of the layout of the current document.
%
%   Additionally to the info provided by that package,
%   with pdfTeX-based engines you also need to know
%
%------------------------------------------------------
\setlength\paperwidth{\the\wd\BoxWithImage}
\setlength\paperheight{\the\ht\BoxWithImage}
\makeatletter
\ifXeTeX
\pdfpagewidth=\paperwidth
\pdfpageheight=\paperheight
\else
\ifLuaTeX
\pagewidth=\paperwidth
\pageheight=\paperheight
\else
\ifPDFTeX
\pdfpagewidth=\paperwidth
\pdfpageheight=\paperheight
\fi
\fi
\ifpdf\else
\GenericWarning{(hint) }{(hint)
--------------------------------------------------------------\MessageBreak
Your TeX-engine seems to be running in dvi-mode.\MessageBreak
In case your image is not an .eps-file, you may need to do\MessageBreak
the following:\MessageBreak
1. Have calculated the bounding box of your image and saved\MessageBreak
\space\space\space the bounding-box-info in a file with extension .bb or .xbb\MessageBreak
\space\space\space by means of a program like ebb (=extract bounding box)\MessageBreak
2. Declare a new graphics-rule, which could, e.g., look like\MessageBreak
\space\space\space this:\MessageBreak
\string\DeclareGraphicsRule{.jpg}{eps}{.bb}{}\MessageBreak
--------------------------------------------------------------\@gobbletwo%
}%
\fi
\fi
\makeatother
\setlength\textwidth{\paperwidth}
\setlength\textheight{\paperheight}
\setlength\evensidemargin{0cm}
\setlength\oddsidemargin{\evensidemargin}
\setlength\marginparwidth{0ex}
\setlength\marginparsep{0ex}
\setlength\topmargin{0cm}
\setlength\footskip{0cm}
\setlength\topskip{0ex}
%------------------------------------------------------
%------------------------------------------------------
\pagestyle{empty}
%------------------------------------------------------
%------------------------------------------------------
\parindent=0ex
\parskip=\baselineskip
%------------------------------------------------------
% Make your image the background of all pages:
%------------------------------------------------------
%------------------------------------------------------
% Let's write the document. Hereby make sure to have
% only one page.
%------------------------------------------------------
\begin{document}
% Put things into a \vbox in order to get overfull-box-
% warnings rather than several pages:
\vbox to \paperheight{%
%----------------------------------------------------
%       Within the \vbox do whatever you like:
%       e.g., use a picture-environment:
%----------------------------------------------------
\setlength{\unitlength}{1cm}%
\begin{picture}(\ConvertToUnitlength{\paperwidth},
\ConvertToUnitlength{\paperheight})(0,0)%
\put(7.7,6.0){%
{\bfseries\sffamily\Large Cogito ergo sum.}%
}%
\end{picture}%
%----------------------------------------------------
}%
\end{document}


I obtained the following pdf-file: