一、圖片引入
在Latex中引入圖片可以使用graphicx包。一般來說,可以使用以下語句引入一張圖片:
\usepackage{graphicx} \includegraphics{image.png}
在引入圖片時,可以設置圖片的大小、位置等屬性。例如下面的代碼設置了圖片寬度為0.8倍頁寬,並且居中顯示。
\begin{center} \includegraphics[width=0.8\textwidth]{image.png} \end{center}
還可以使用\textbackslash{}includegraphics命令指定圖片位置,例如:
\begin{figure}[htbp] \centering \includegraphics[width=0.8\textwidth]{image.png} \caption{圖片描述} \label{fig:image} \end{figure}
這裡的htbp表示:h – 圖片放在當前位置,t – 圖片放在頁面頂部,b – 圖片放在頁面底部,p – 圖片放在一頁專門用於放置浮動對象的頁面上。
二、圖片居中
居中是最常用的圖片對齊方式,通常使用center環境實現。例如:
\begin{center} \includegraphics{image.png} \end{center}
在LaTeX中,默認情況下將圖片放在當前行的基線上。如果圖片很大,則可能會導致行距不均勻。我們可以使用\textbackslash{}raisebox命令來調整圖片的垂直位置,如下:
\begin{center} \raisebox{-.5\height}{\includegraphics{image.png}} \end{center}
這裡的-.5\textbackslash{}height表示圖片中心垂直距離當前行基線的距離。當然,也可以根據實際情況來調整這個值。
三、圖片居左/居右
圖片居左和居右通常使用wrapfig宏包實現。例如,下面的代碼將圖片居左,並且將圖片和文字分別放在兩個欄中:
\usepackage{wrapfig} \begin{wrapfigure}{l}{0.4\textwidth} \includegraphics[width=0.4\textwidth]{image.png} \caption{圖片描述} \label{fig:image} \end{wrapfigure} 這是要和圖片並排放的內容。
其中,wrapfigure環境的第一個參數{l/r}表示圖片的對齊方式。l表示圖片居左,r表示圖片居右。第二個參數{0.4\textbackslash{}textwidth}表示圖片寬度為0.4倍列寬。注意,wrapfigure環境只能在段落開頭或者下一頁開始布局,並且不能嵌套使用。
四、圖片到頁邊的距離
在Latex中,可以使用geometry宏包調整頁面布局。例如,下面的代碼將頁面設置為A4大小,並將圖片距離左右邊緣各1cm:
\usepackage{geometry} \newgeometry{left=1cm,right=1cm,top=2cm,bottom=2cm} \begin{figure}[htbp] \centering \includegraphics[width=0.8\textwidth]{image.png} \caption{圖片描述} \label{fig:image} \end{figure} \restoregeometry
其中,newgeometry用於設置新的頁面布局,而restoregeometry則用於恢復原來的頁面布局。
五、圖片放置位置的調整
有時候我們需要根據內容的需要來調整圖片的放置位置。此時,可以使用minipage環境。例如,下面的代碼將一張圖片分為兩個部分放置在兩個minipage中,並且放置位置向內容的底部靠近:
\begin{figure}[htbp] \begin{minipage}[b]{0.48\linewidth} \centering \includegraphics[width=\linewidth]{image1.png} \caption{圖片1} \label{fig:image1} \end{minipage} \hfill \begin{minipage}[b]{0.48\linewidth} \centering \includegraphics[width=\linewidth]{image2.png} \caption{圖片2} \label{fig:image2} \end{minipage} \end{figure}
其中,minipage環境的第一個參數[b/t/c]表示minipage的對齊方式。b表示底部對齊,t表示頂部對齊,c表示居中對齊。第二個參數{0.48\textbackslash{}linewidth}表示minipage的寬度為當前行寬度的48%。
原創文章,作者:HSOFA,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/366250.html