LaTex 插入图像

插入图像


图像是大多数科学文献中必不可少的元素。LATEX提供了几个选项来处理图像并使它们看起来完全符合您的需要。在本文中,我们将解释如何以最常见的格式包含图像,如何缩小、放大和旋转它们,以及如何在文档中引用它们。

介绍

以下是有关如何导入图片的示例。

\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage[rightcaption]{sidecap}
\graphicspath{ {./image/} }

%\graphicspath{ {E:/work/Latex/image/} }

\begin{document}
	The universe is immense and it seems to be homogeneous, 
	in a large scale, everywhere we look at.
	
	\includegraphics{universe.jpg}
	
	
	There's a picture of a galaxy above
\end{document}

\end{document}

在这里插入图片描述

Latex 不能自己管理图片,所以我们需要使用graphicx包。要使用它,我们在序言中包含以下行:\usepackage{graphicx}.

该命令\graphicspath{ {./images/} }告诉LATEX图像保存在images主文档目录下的一个文件夹中。

\includegraphics{seal}命令是在文档中实际包含图像的命令。seal是包含没有扩展名的图像的文件的名称,然后seal.PNG变为universe. 图像的文件名不应包含空格或多个点。

注意:允许包含文件扩展名,但最好省略它。如果省略文件扩展名,它将提示 LaTeX 搜索所有支持的格式。有关详细信息,请参阅有关生成高分辨率和低分辨率图像的部分。

图片的文件夹路径

在处理包含多个图像的文档时,可以将这些图像保存在一个或多个单独的文件夹中,以便您的项目更有条理。

该命令\graphicspath{ {images/} }告诉LATEX在images文件夹中查找。该路径是相对于当前工作目录的——因此,编译器将在与包含图像的代码相同的文件夹中查找文件。文件夹的路径默认是相对的,如果没有指定初始目录,例如

%Path relative to the .tex file containing the \includegraphics command
\graphicspath{ {images/} }

这是访问文件树中图形文件夹的一种典型的直接方式,但当.tex文件夹中的文件包含在邮件.tex文件中时可能会导致复杂性。然后,编译器可能最终会在错误的位置寻找图像文件夹。因此,最好将图形路径指定为相对于主.tex文件,将主.tex文件目录表示为 ./ ,例如:

%Path relative to the main .tex file 
\graphicspath{ {./images/} }

就像在介绍中一样。

如果指定了系统上文件的确切位置,则路径也可以是绝对路径。例如,如果您在自己的计算机上安装本地 LaTeX:

%Path in Windows format:
\graphicspath{ {c:/user/images/} }

%Path in Unix-like (Linux, Mac OS) format
\graphicspath{ {/home/user/images/} }

请注意,此命令需要尾部斜杠 / ,并且路径位于双括号之间。

如果图像保存在多个文件夹中,您还可以设置多个路径。例如,如果有两个名为images1and的文件夹images2,请使用命令

\graphicspath {  { ./images1/ }{ ./images2/ }  }

更改图像大小和旋转图片

如果我们想进一步指定LATEX应该如何在文档中包含我们的图像(长度、高度等),我们可以按以下格式传递这些设置:

\documentclass{article}
\usepackage{graphicx}

\graphicspath{ {./image/} }

%\graphicspath{ {E:/work/Latex/image/} }

\begin{document}

	
	Overleaf is a great professional tool to edit online documents, 
	share and backup your \LaTeX{} projects. Also offers a 
	rather large help documentation.
	
	\includegraphics[scale=1.5]{overleaf-logo}
	
	
\end{document}

在这里插入图片描述

该命令\includegraphics[scale=1.5]{overleaf-logo}将在文档中包含图像overleaf-logo,额外的参数scale=1.5将执行此操作,将图像缩放为实际大小的 1.5。

您还可以将图像缩放到某个特定的宽度和高度。

	
\documentclass{article}
\usepackage{graphicx}

\graphicspath{ {./image/} }

%\graphicspath{ {E:/work/Latex/image/} }

\begin{document}

	

Overleaf is a great professional tool to edit online documents, 
share and backup your \LaTeX{} projects. Also offers a 
rather large help documentation.

\includegraphics[width=5cm, height=4cm]{overleaf-logo}
	
\end{document}

在这里插入图片描述

您可能已经猜到了,括号内的参数[width=3cm, height=4cm]定义了图片的宽度和高度。您可以为这些参数使用不同的单位。如果只传递了宽度参数,高度将被缩放以保持纵横比。

长度单位也可以相对于文档中的某些元素。例如,如果您想制作与文本宽度相同的图片:

\documentclass{article}
\usepackage{graphicx}

\graphicspath{ {./image/} }

%\graphicspath{ {E:/work/Latex/image/} }

\begin{document}

	
	
	The universe is immense and it seems to be homogeneous, 
	in a large scale, everywhere we look at.
	
	\includegraphics[width=\textwidth]{seal}
	
	
\end{document}

\textwidth您可以使用任何其他默认LATEX长度代替: \columnsep、\linewidth、\textheight、\paperheight等。有关这些单位的进一步说明,请参阅参考指南。

在文档中包含图片时,还有另一个常见的选项是旋转它。这可以在LATEX中轻松完成:

\documentclass{article}
\usepackage{graphicx}

\graphicspath{ {./image/} }

%\graphicspath{ {E:/work/Latex/image/} }

\begin{document}

Overleaf is a great professional tool to edit online, 
share and backup your \LaTeX{} projects. Also offers a 
rather large base of help documentation.

\includegraphics[scale=0.9, angle=45]{overleaf-logo}
	
	
\end{document}

在这里插入图片描述

定位

在上一节中解释了如何在文档中包含图像,但是文本和图像的组合可能看起来不像我们预期的那样。要改变这一点,我们需要引入一个新环境。

	
\documentclass{article}
\usepackage{graphicx}

\graphicspath{ {./image/} }

%\graphicspath{ {E:/work/Latex/image/} }

\begin{document}

In the next example the figure will be positioned 
right below this sentence.

\begin{figure}[h]
	\includegraphics[width=8cm]{Plot}
\end{figure}
	
	
\end{document}

在这里插入图片描述

figure环境用于将图片显示为文档中的浮动元素。这意味着您将图片包含在figure环境中,您不必担心它的位置,LATEX会以适合文档流的方式定位它。

无论如何,有时我们需要对图形的显示方式进行更多控制。可以传递一个附加参数来确定图形定位。在示例中,begin{figure}[h]括号内的参数将图形的位置设置为此处。下表列出了可能的定位值。

范围位置
h将浮动放置在此处,即大约在源文本中出现的同一点(但是,不完全在该位置)
t位于页面顶部。
b位置在页面底部。
p只为花车放置一个特殊页面。
!覆盖 LaTeX 用于确定“良好”浮动位置的内部参数。
H将浮点数精确地放置在LATEX代码中的位置。需要该float软件包,但有时可能会导致问题。这有点等价于h!.

在下一个示例中,您可以t在文档的操作部分看到一张图片,尽管它是在文本下方声明的。

\documentclass{article}
\usepackage{graphicx}

\graphicspath{ {./image/} }

%\graphicspath{ {E:/work/Latex/image/} }

\begin{document}

In this picture you can see a bar graph that shows
the results of a survey which involved some important
data studied as time passed.

\begin{figure}[t]
	\includegraphics[width=8cm]{Plot}
	\centering
\end{figure}
	
	
\end{document}

在这里插入图片描述

附加命令\centering将使图片居中。默认对齐方式为left。

也可以将文本环绕在图形周围。当文档包含小图片时,这会使它看起来更好。

	
\documentclass{article}
\usepackage{graphicx}
\usepackage{wrapfig}

\graphicspath{ {./image/} }

%\graphicspath{ {E:/work/Latex/image/} }

\begin{document}

\begin{wrapfigure}{r}{0.25\textwidth} %this figure will be at the right
	\centering
	\includegraphics[width=0.25\textwidth]{mesh}
\end{wrapfigure}

There are several ways to plot a function of two variables, 
depending on the information you are interested in. For 
instance, if you want to see the mesh of a function so it 
easier to see the derivative you can use a plot like the 
one on the left.


\begin{wrapfigure}{l}{0.25\textwidth}
	\centering
	\includegraphics[width=0.25\textwidth]{contour}
\end{wrapfigure}

On the other side, if you are only interested on
certain values you can use the contour plot, you 
can use the contour plot, you can use the contour 
plot, you can use the contour plot, you can use 
the contour plot, you can use the contour plot, 
you can use the contour plot, like the one on the left.

On the other side, if you are only interested on 
certain values you can use the contour plot, you 
can use the contour plot, you can use the contour 
plot, you can use the contour plot, you can use the 
contour plot, you can use the contour plot, 
you can use the contour plot, 
like the one on the left.
	
\end{document}

在这里插入图片描述

要使示例中的命令正常工作,您必须导入包wrapfig。将这一行添加到序言中\usepackage{wrapfig}

现在您可以wrapfigure通过命令定义环境\begin{wrapfigure}{l}{0.25\textwidth} \end{wrapfigure}。请注意,环境有两个附加参数括在大括号中。下面对代码进行了更详细的解释:

{l}
这定义了图形的对齐方式。将l设置为左对齐,r设置为右对齐。此外,如果您使用书籍或任何类似格式,请使用o代替页面的外边缘,而使用i代替页面的内边缘。
{0.25\textwidth}
这是图形框的宽度。这不是图像本身的宽度,必须在\includegraphics命令中设置。请注意,长度与文本宽度相关,但也可以使用标准单位(厘米、英寸、毫米等)。有关单位列表,请参阅参考指南。
\centering
这已经解释过了,但是在这个例子中,图像将通过使用它的容器作为参考来居中,而不是整个文本。
有关图像定位的更完整文章,请参阅定位图像和表格

字幕、标签和参考

在处理冗长的文本时,为图像添加简短描述并标记它们以供进一步参考是两个重要的工具。

字幕

让我们从一个标题示例开始:

\documentclass{article}
\usepackage{graphicx}
\usepackage{wrapfig}

\graphicspath{ {./image/} }

%\graphicspath{ {E:/work/Latex/image/} }

\begin{document}

\begin{figure}[h]
	\caption{Example of a parametric plot ($\sin (x), \cos(x), x$)}
	\centering
	\includegraphics[width=0.5\textwidth]{spiral}
\end{figure}
	
\end{document}

在这里插入图片描述

这真的很简单,只需在\caption{ }的大括号内添加要显示的文本即可。标题的位置取决于您放置命令的位置;如果它在上面,\includegraphics那么标题将在它的顶部,如果它在下面,那么标题也将设置在图的下方。

标题也可以放在数字之后。该sidecap包使用与上一个示例中的代码类似的代码来完成此操作。

\documentclass{article}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage[rightcaption]{sidecap}
\graphicspath{ {./image/} }

%\graphicspath{ {E:/work/Latex/image/} }

\begin{document}

\begin{SCfigure}[0.5][h]
	\caption{Using again the picture of the universe.
		This caption will be on the right}
	\includegraphics[width=0.6\textwidth]{universe.jpg}
\end{SCfigure}
	
\end{document}

在这里插入图片描述

有两个新命令

\usepackage[rightcaption]{sidecap}
正如你所料,这一行将导入一个名为sidecap的包,但还有一个附加参数:rightcaption. 此参数确定图片右侧标题的位置,您也可以使用leftcaption. 在类似书本的文件outercaptioninnercaption也有。这些名称是自我描述的。
\begin{SCfigure}[0.5][h] \end{SCfigure}
定义类似于figure的环境。第一个参数是相对于图像大小的标题宽度,如\includegraphics. 第二个参数的工作方式与环境h中的完全相同。有关figure更多信息,请参阅放置部分。
您可以对字幕格式进行更高级的管理。检查进一步阅读部分以获取参考。

标签和交叉引用

图,就像LATEX文档中的许多其他元素(方程式、表格、绘图等)一样,可以在文本中引用。这很简单,只需在figureorSCfigure环境中添加一个\label,然后使用该标签来引用图片。

\documentclass{article}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage[rightcaption]{sidecap}
\graphicspath{ {./image/} }

%\graphicspath{ {E:/work/Latex/image/} }

\begin{document}

\begin{figure}[h]
	\centering
	\includegraphics[width=0.25\textwidth]{mesh}
	\caption{a nice plot}
	\label{fig:mesh1}
\end{figure}

As you can see in the figure \ref{fig:mesh1}, the 
function grows near 0. Also, in the page \pageref{fig:mesh1} 
is the same example.

\end{document}

在这里插入图片描述

在此示例中,有三个命令生成交叉引用。

\label{fig:mesh1}
这将为该图设置一个标签。由于标签可用于文档中的多种类型的元素,因此最好使用前缀,例如fig:在示例中。
\ref{fig:mesh1}
此命令将插入分配给图形的编号。它是自动生成的,如果在引用的图形之前插入一些其他图形,它将被更新。
\pageref{fig:mesh1}
这将打印出引用图像出现的页码。
\caption必须引用一个数字 。

LATEX文档的另一个重要特点是能够自动生成图形列表。这很简单。

\listoffigures

此命令仅适用于带标题的数字,因为它使用表格中的标题。上面的示例列出了本文中的图像。

重要提示:当使用交叉引用时,您的LATEX项目必须编译两次,否则引用、页面引用和图表将不起作用 - Overleaf 会为您解决这些问题。

生成高分辨率和低分辨率图像

到目前为止,在\includegraphics命令中指定图像文件名时,我们省略了文件扩展名。但是,这不是必需的,尽管它通常很有用。如果省略文件扩展名,LaTeX 将在该目录中搜索任何支持的图像格式,并以默认顺序(可以修改)搜索各种扩展名。

这对于在开发和生产环境之间切换很有用。在开发环境中(当文章/报告/书籍仍在进行中时),最好使用低分辨率版本的图像(通常为.png格式)来快速编译预览。在生产环境中(当产生文章/报告/书籍的最终版本时),最好包含高分辨率版本的图像。

这是通过

  • 未在\includegraphics命令中指定文件扩展名,并且
  • 在序言中指定所需的扩展名。
    因此,如果我们有两个版本的图像,venndiagram.pdf(高分辨率)和 venndiagram.png(低分辨率),那么我们可以在序言中包含以下行以在开发报告时使用 .png 版本
 \DeclareGraphicsExtensions { .png,.pdf }

上面的命令将确保如果遇到两个具有相同基本名称但扩展名不同的文件(例如 venndiagram.pdf 和 venndiagram.png),则首先使用 .png 版本,如果没有,则使用 .pdf 版本使用,如果某些低分辨率版本不可用,这也是一个好主意。

开发报告后,要使用高分辨率 .pdf 版本,我们可以将序言中指定扩展搜索顺序的行更改为

  \DeclareGraphicsExtensions { .pdf,.png }

改进前面段落中描述的技术,如果存在尚未转换为 PNG 的 PDF ,我们还可以指示LATEX在编译文档时动态生成低分辨率 .png 版本的图像。为了实现这一点,我们可以在序言中包含以下内容\usepackage{graphicx}

\usepackage { epstopdf } 
\epstopdfDeclareGraphicsRule { .pdf }{ png }{ .png }{转换 #1 \OutputFile } 
\DeclareGraphicsExtensions { .png,.pdf }

如果 venndiagram2.pdf 存在但不存在 venndiagram2.png,则将创建文件 venndiagram2-pdf-converted-to.png 并在其位置加载。命令convert #1负责转换,并且可以在convert和#1之间传递其他参数。例如 -转换 -density 100 #1。

不过,有一些重要的事情要记住:

为了使自动转换起作用,我们需要使用选项调用pdflatex–shell-escape。
对于最终的生产版本,我们必须注释掉\epstopdfDeclareGraphicsRule,以便只加载高分辨率的 PDF 文件。我们还需要更改优先顺序。

参考

LATEX单位和长度

缩写定义
pt一个点,是默认的长度单位。约0.3515mm
mm一毫米
cm一厘米
in一英寸
ex当前字体中x的高度
em当前字体中m的宽度
\columnsep行间距离
\columnwidth列宽
\linewidth当前环境中线的宽度
\paperwidth页面宽度
\paperheight页面高度
\textwidth文字宽度
\textheight文字高度
\unitlength图片环境中的长度单位。

关于LATEX中的图像类型

latex

使用latex编译时,我们只能使用 EPS 图像,这是一种矢量格式。

pdflatex

如果我们使用“pdflatex”编译生成 PDF,那么我们可以使用多种图像格式 -

  • JPG:如果我们要插入照片,最好的选择
  • PNG:如果我们想要插入图表(如果无法生成矢量版本)和屏幕截图的最佳选择
  • PDF:尽管我们习惯于查看 PDF 文档,但 PDF 也可以存储图像
  • EPS:可以使用epstopdf包包含 EPS 图像(我们只需要安装包,我们不需要使用\usepackage{}将其包含在我们的文档中。)
矢量格式还是位图格式?

图像可以是矢量格式或位图格式。通常我们不需要担心它,但是如果我们碰巧知道图像的格式,我们可以使用该信息来选择适当的图像格式以包含在我们的 LaTeX 文档中。如果我们有矢量格式的图像,我们应该选择 PDF 或 EPS。如果我们有位图格式,我们应该选择 JPG 或 PNG,因为将位图图片存储为 PDF 或 EPS 会占用大量磁盘空间。

  • 7
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值