我正在向作品中添加图形,但大多数图形太宽,因此没有居中,而是向右移动。我在这里搜索并找到了一个解决方案,即\centerline{}
在图形环境中添加命令。对于使用命令添加的图形,此方法很有效\includegraphics{}
,如本例所示:
\begin{figure}[h!]
\begin{center}
\centerline{\includegraphics{SOIR_optics}}
\end{center} % is this really useful?
\end{figure}
但是当我尝试添加时tikzpicture
,此解决方案不起作用(参见示例)
\begin{figure}[h!]
\begin{center}
\centerline{\begin{tikzpicture}
some code
\end{tikzpicture}}
\end{center}
\end{figure}
我的问题是:这个\centerline{}
命令还有其他选择吗?
谢谢!
答案1
该\centerline
命令永远不应在 LaTeX 文档中使用(除非你清楚地知道自己在做什么,并且可能只在某些定义的序言中)。使用
\begin{figure}[htp]
\centering
<whatever>
\end{figure}
并且<whatever>
(图形、TikZ 图片或任何东西)将居中。
答案2
您可以使用adjustbox
\documentclass{scrreprt}
\usepackage[export]{adjustbox} %% export option makes adjustbox --
%% -- goodies available inside includegraphics command
\usepackage{graphicx}
\begin{document}
X\hrulefill X
\begin{figure}[htp]
\includegraphics[width=1.1\textwidth,center]{example-image-a}
\end{figure}
\end{document}
这对于宽度大于的图形非常有用\textwidth
。另一个有用的宏是adjustbox
带有选项的环境max width
。
\documentclass{scrreprt}
\usepackage[export]{adjustbox}
\usepackage{graphicx}
\begin{document}
X\hrulefill X
This is resized to \verb|1.1\textwidth|
\begin{figure}[htp]
\begin{adjustbox}{center,max width=1.1\textwidth}
\includegraphics[width=1.2\textwidth,center]{example-image-a}
\end{adjustbox}
\end{figure}
\clearpage
The following is not resized:
\begin{figure}[htp]
\begin{adjustbox}{center,max width=1.1\textwidth}
\includegraphics[width=0.7\textwidth,center]{example-image-b}
\end{adjustbox}
\end{figure}
\end{document}
的优点max width
是,只有当内容超出时才会调整大小,max width
否则不会。
有tikzpicture
环境
我假设你的 tikz 图片被保存为单独的文件。然后使用adjustbox
和tikzscale
包:
\documentclass{article}
\usepackage{graphicx,tikz,tikzscale}
\usepackage{adjustbox}
\usepackage{filecontents}
\begin{filecontents*}{myfig.tikz}
\begin{tikzpicture}[inner sep=0pt, outer sep=0pt]
\draw[use as bounding box](-20,-20) rectangle (20,20);
\node at (0,0) (A) {A};
\node[above right] (B) at (A.north east) {B};
\draw (A.south west)--(B.north east);
\end{tikzpicture}
\end{filecontents*}
\begin{document}
X\hrulefill X
This is resized to \verb|1.1\textwidth|
\begin{figure}[htp]
\begin{adjustbox}{max width=1.1\textwidth,center}
\includegraphics[width=1.2\textwidth]{myfig.tikz}
\end{adjustbox}
\end{figure}
\clearpage
The following is not resized:
\begin{figure}[htp]
\begin{adjustbox}{max width=1.1\textwidth,center}
\includegraphics[width=0.7\textwidth]{myfig.tikz}
\end{adjustbox}
\end{figure}
\end{document}