对于我正在撰写的一篇论文,我使用 Mathematica 生成了一堆图表(pdf 文件),我使用 LaTeX 中的 \put 命令对其进行了标记。但是,我现在想将标记的图表保存为 pdf 文件。
另一种标记方法是使用图像编辑器(例如 Inkscape 或 GIMP)打开每个 pdf 文件,然后使用 LaTeXit 手动添加 LaTeX 符号,但我真的不想这样做。还有一种方法是将标签移植到我的 Mathematica 代码中,但我还是不想这样做。
有没有办法将用 LaTeX 代码额外修饰的 pdf 图像保存为新的 pdf 图像?
答案1
(以下帖子并未回答您关于从文档中提取标记图像的问题。)
另一种从无标记图像中获取标记图像作为额外 pdf 文件(进而可以通过 包含在您的文档中)的方法是\includegraphics
,将您的标记图像保存为单独的 LaTeX 文档,该文档由一页组成,该页的尺寸对应于底层图像的尺寸,并缩放到图像在文档中显示的大小。
使用下面的模板,\includegraphics
在一个框内进行。
该框既用于确定文档页面的尺寸,也用于将图像作为背景图像。
我下载了https://images.pexels.com/photos/247431/pexels-photo-247431.jpeg并将其保存为大象.jpg。
(Pexels 上的照片可以免费使用,更多信息请访问https://www.pexels.com/creative-commons-images/)
完成后,我可以使用以下模板创建带标签的 pdf 文件——我使用 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}
%------------------------------------------------------
% Load whatever other packages you need for adjusting
% 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{%
%-------------------------------------------------
% Load your image via \includegraphics:
%-------------------------------------------------
\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
% about \pdfpagewidth and \pdfpageheight.
%
%------------------------------------------------------
\setlength\paperwidth{\the\wd\BoxWithImage}
\setlength\paperheight{\the\ht\BoxWithImage}
\addtolength\paperheight{\the\dp\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}
\addtolength\evensidemargin{-1in}
\addtolength\evensidemargin{-\hoffset}
\setlength\oddsidemargin{\evensidemargin}
\setlength\marginparwidth{0ex}
\setlength\marginparsep{0ex}
\setlength\topmargin{0cm}
\addtolength\topmargin{-1in}
\addtolength\topmargin{-\voffset}
\setlength\headheight{0cm}
\setlength\headsep{0cm}
\setlength\footskip{0cm}
\setlength\topskip{0ex}
%------------------------------------------------------
% Adjust the page-style:
%------------------------------------------------------
\pagestyle{empty}
%------------------------------------------------------
% Adjust \parindent and \parskip
%------------------------------------------------------
\parindent=0ex
\parskip=\baselineskip
%------------------------------------------------------
% Make your image the background of all pages:
%------------------------------------------------------
\AddToShipoutPicture{\copy\BoxWithImage}
%------------------------------------------------------
% 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}
我获得了以下pdf文件:
现在我可以通过 在我的文档中使用它\includegraphics
。
我认为,调整模板您需要做的就是调整命令,\includegraphics
以便获得具有正确测量值的正确背景图像,并调整内容\vbox to \paperheight
。
例如,改变的内容,\vbox to \paperheight
使其包含一个图片环境,我可以在其中使用\put
,结果如下:
%------------------------------------------------------
% 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}
%------------------------------------------------------
% Load whatever other packages you need for adjusting
% 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{%
%-------------------------------------------------
% Load your image via \includegraphics:
%-------------------------------------------------
\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
% about \pdfpagewidth and \pdfpageheight.
%
%------------------------------------------------------
\setlength\paperwidth{\the\wd\BoxWithImage}
\setlength\paperheight{\the\ht\BoxWithImage}
\addtolength\paperheight{\the\dp\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}
\addtolength\evensidemargin{-1in}
\addtolength\evensidemargin{-\hoffset}
\setlength\oddsidemargin{\evensidemargin}
\setlength\marginparwidth{0ex}
\setlength\marginparsep{0ex}
\setlength\topmargin{0cm}
\addtolength\topmargin{-1in}
\addtolength\topmargin{-\voffset}
\setlength\headheight{0cm}
\setlength\headsep{0cm}
\setlength\footskip{0cm}
\setlength\topskip{0ex}
%------------------------------------------------------
% Adjust the page-style:
%------------------------------------------------------
\pagestyle{empty}
%------------------------------------------------------
% Adjust \parindent and \parskip
%------------------------------------------------------
\parindent=0ex
\parskip=\baselineskip
%------------------------------------------------------
% Make your image the background of all pages:
%------------------------------------------------------
\AddToShipoutPicture{\copy\BoxWithImage}
%------------------------------------------------------
% 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}
我获得了以下pdf文件: