我尝试显示被文本包裹的图像,但无法精确对齐它们。图像始终显示在文本块开头下方一行。此外,即使我想将其与 \subsection 对齐,它也会显示在下方一行。
我尝试过通过 \vspace{-33pt} 移动图像,但只有当 \wrapfigure 指定了宽度时才有效。当我需要使用 0pt 宽度设置检测 \wrapfigure 宽度时,此设置完全被忽略。 (请不要对我的示例中指定的图像(=规则)宽度感到困惑 - 实际文档包含数百个可变宽度的图像,因此不能将宽度指定为参数,并且必须真正检测到 \wrapfigure 宽度。)
如何正确对齐图像顶部
1) \subsection 标题
2)文本第一行
对 \wrapfigure 宽度检测有额外要求吗?
以下是完整的非工作代码和描述观察到的和预期的行为的屏幕截图。
\documentclass[a4paper]{book}
\usepackage{fontspec}
\usepackage{polyglossia}
\setmainlanguage{czech}
\usepackage{ulem}
\usepackage{spverbatim}
\usepackage[unicode]{hyperref}
\usepackage[]{graphicx}
\usepackage{grffile}
\usepackage{color}
\usepackage{wrapfig}
\usepackage{hologo}
\usepackage{pdfpages}
\usepackage{needspace}
\begin{document}
\chapter{Chapter 1}
\section{Section 1}
\needspace{6cm}
\begin{wrapfigure}{R}{0pt}
%\vspace{-33pt} % Doesn't work for width detection (0pt above)
\centering
\rule{3cm}{4cm}
%\includegraphics[height=4cm,width=3cm]{img.jpg}
\end{wrapfigure}
\subsection{Subsection 1} % <----- Image shall be here, but is one line below Some text
Some text below. Some text below. Some text below. Some text below. Some text below. Some text below. Some text below. Some text below.
\chapter{Chapter 2}
\section{Section 2}
\subsection{Subsection 2}
\needspace{6cm}
\begin{wrapfigure}{R}{0pt}
\centering
\rule{3cm}{4cm}
%\includegraphics[height=4cm,width=3cm]{img.jpg}
\end{wrapfigure}
Some text below. Some text below. Some text below. Some text below. Some text below. Some text below. Some text below. Some text below.
\end{document}
答案1
您可以将 \intextsep 设置为 0,以便与段落的第一行对齐。要将其进一步向上移动,例如在标题中使用 \raisebox,并使用选项参数隐藏实际高度。请注意,长标题将被此类图像覆盖。
\documentclass[a4paper]{book}
\usepackage{wrapfig}
\usepackage{needspace}
\intextsep=0pt
\begin{document}
\chapter{Chapter 1}
\section{Section 1}
\needspace{6cm}
\begin{wrapfigure}{R}{0pt}
\centering
\raisebox{\baselineskip}[\dimexpr\height-\baselineskip][0pt]{\rule{3cm}{4cm}}
\end{wrapfigure}
\subsection{Subsection 1} % <----- Image shall be here, but is one line below Some text
Some text below. Some text below. Some text below. Some text below. Some text below. Some text below. Some text below. Some text below.
\chapter{Chapter 2}
\section{Section 2}
\subsection{Subsection 2}
\needspace{6cm}
\begin{wrapfigure}{R}{0pt}
\centering
\rule{3cm}{4cm}
\end{wrapfigure}
Some text below. Some text below. Some text below. Some text below. Some text below. Some text below. Some text below. Some text below.
\end{document}
答案2
你可以玩一下\raisebox
,通过命令的第一个可选参数用图像的真实高度欺骗 LaTeX:
\documentclass[a4paper]{book}
\usepackage{fontspec}
\usepackage{polyglossia}
\setmainlanguage{czech}
\usepackage{ulem}
\usepackage{spverbatim}
\usepackage[unicode]{hyperref}
\usepackage[demo]{graphicx}
\usepackage{grffile}
\usepackage{color}
\usepackage{wrapfig}
\usepackage{hologo}
\usepackage{pdfpages}
\usepackage{needspace}
\usepackage{lipsum}
\begin{document}
\chapter{Chapter 1}
\section{Section 1}
\needspace{6cm}
\begin{wrapfigure}{R}{0pt}
%\vspace{-33pt} % Doesn't work for width detection (0pt above)
\centering
\rule{3cm}{4cm}
%\includegraphics[height=4cm,width=3cm]{img.jpg}
\end{wrapfigure}
\subsection{Subsection 1} % <----- Image shall be here, but is one line below Some text
Some text below. Some text below. Some text below. Some text below. Some text below. Some text below. Some text below. Some text below.
\chapter{Chapter 2}
\section{Section 2}
\subsection{Subsection 2}
\needspace{6cm}
\begin{wrapfigure}[8]{R}{0pt}
\centering
\raisebox{4ex}[0.8\height]{ \includegraphics[height=4cm,width=3cm]{img.jpg}}
\end{wrapfigure}
\leavevmode \lipsum[1]
\end{document}