如何在 tex 文件中插入 (EPS/JPEG/PNG) 格式的图片?

如何在 tex 文件中插入 (EPS/JPEG/PNG) 格式的图片?

我有一个 .tex 文件,内容如下:

\NeedsTeXFormat{LaTeX2e}
\documentclass[border=10pt]{standalone}
\usepackage{fontspec}
\newcommand*{\NewFont}[1]{%
  \expandafter\newfontfamily\csname#1\endcsname{#1}%
}
\newcommand*{\SetFont}[1]{%
  \csname#1\endcsname
  \ignorespaces
}
\newdimen\FontSize
\newdimen\FontUnitLength
\setlength{\FontUnitLength}{.75bp}
\newcommand*{\SetSize}[1]{%
  \setlength{\FontSize}{#1\FontUnitLength}%
  \fontsize{\FontSize}{1.2\FontSize}\selectfont
  \ignorespaces
}
\usepackage{graphicx}
\newcommand*{\Put}{}
\def\Put(#1,#2)(#3,#4)#5{%
  %\put(#1,#2){\makebox(#3,#4)[lb]{\resizebox{#3\unitlength}{!}{#5}}}%
  \put(#1,#2){%
    \makebox(#3,#4)[lb]{%
      \resizebox{#3\unitlength}{#4\unitlength}{%
        \raisebox{\depth}{#5}%
      }%
    }%
  }%
  %\put(#1,#2){\makebox(#3,#4)[lb]{#5}}%
}
\setlength{\unitlength}{1bp}
\NewFont{Trebuchet_MS_Bold}
\NewFont{Verdana_Bold}
\NewFont{Georgia}
\NewFont{Arial}
\NewFont{Trebuchet_MS}
\NewFont{Verdana}
\NewFont{Verdana_Bold_Italic}
\NewFont{Trebuchet_MS_Italic}
\NewFont{Arial_Bold}
\NewFont{Times_New_Roman_Bold_Italic}
\NewFont{Georgia_Italic}
\NewFont{Times_New_Roman_Italic}
\NewFont{Verdana_Italic}
\NewFont{Times_New_Roman}
\NewFont{Courier_New}
\begin{document}
\begin{picture}(939,1287)
\SetFont{Courier_New}
\SetSize{48}
\Put(12,1249)(131,36){Swine}
\Put(159,1249)(55,38){flu}
\Put(230,1240)(181,47){tightens}
\Put(428,1249)(49,36){its}
\Put(492,1240)(84,45){grip}
\Put(592,1249)(99,27){over}
\Put(708,1249)(105,38){India}
\SetSize{19}
\Put(589,1192)(53,14){Swine}
\Put(649,1192)(23,14){flu}
\Put(681,1192)(29,14){has}
\Put(716,1188)(88,18){tightened}
\Put(813,1192)(19,14){its}
\Put(839,1188)(34,14){grip}
\Put(880,1192)(41,10){over}
\SetSize{17}
\Put(133,1156)(1,21){j}
\Put(590,1158)(312,18){India,withthedeathtollreaching}
\SetSize{20}
\Put(589,1131)(45,14){close}
\Put(641,1131)(18,13){to}
\Put(668,1131)(48,14){1000.}
\Put(725,1131)(47,14){Fresh}

和更多......

我使用上述 latex 文件最终创建了一个 PDF,其中单词位于使用 put 命令的特定 XY 坐标处(事先已知)。现在,除此之外,我还想将图片(存储为单独的图像文件,名称为 1.jpg、2.jpg...)包含在特定的 XY 坐标中,就像我对单词所做的那样。

我所做的是,在 .tex 文件中插入此行(在 Swine 一词的定位之前): \Put(415,1171){\includegraphics[width=515, height=243{/home/John/Desktop/1.jpg}}

这里,(415,1171) 是坐标,路径是图像的位置。当我尝试将上述 tex 文件转换为 PDF 时,它显示错误(没有它工作得完全正常)并给出以下错误并停止:

</home/John/Desktop/1.jpg, id=1, 516.93124pt x 243.91125pt>
! Illegal unit of measure (pt inserted).
<to be read again> 

答案1

Textpos包可用于此目的。元素的定位需要大量解释,因此以下代码仅供参考。示例中的文本框相对于边距显示,边距是使用包的 showframe 选项生成的geometry

\documentclass[border=10pt,a4paper]{article}
\usepackage[lmargin=24mm,rmargin=24mm,tmargin=25mm,bmargin=25mm,showframe]{geometry} %Set margins and show them
\usepackage[absolute,showboxes]{textpos} % Positioning textblocks in absolute terms
\usepackage{graphicx}
\usepackage{mwe}

\setlength{\TPHorizModule}{1mm}
\setlength{\TPVertModule}{1mm}

\setlength{\parindent}{0pt}

\begin{document}

\begin{textblock}{2}(24,25) % Place a * at the intersection at the top and left margins
    $*$ %Top left
\end{textblock}

\begin{textblock}{40}(140,50) %Place an image in a box 40 mm wide 140 mm from the left edge and 50 mm from the top
    \centering
    \includegraphics[scale=0.20]{example-image}%From mwe package
\end{textblock}

\begin{textblock}{20}(24,100) %Place a textblock on the left margin (24 mm), 100 mm from the top.
Swine \\
flu \\
tightens \\
its \\
grip \\
over \\
India
\end{textblock}

\begin{textblock}{40}(90,120) %Place a textblock 90 mm from the left edge and 120 mm from the top
\raggedleft
Swine \\
flu \\
tightens \\
its \\
grip \\
over \\
India
\end{textblock}

\begin{textblock}{2}(186,272) % Place a * at the intersection of the bottom and right margins
     $*$ %Bottom right
\end{textblock}

\end{document}

在此处输入图片描述

相关内容