如何防止在正文中插入图形后出现换行

如何防止在正文中插入图形后出现换行

我使用“h”浮点说明符将图形插入正文中。在下面的示例中,由于当前页面 (1) 上没有足够的空间,因此图形被放置在下一页 (2) 的开头。

当前代码:

\documentclass[11pt, oneside, letterpaper]{book}

\usepackage{graphicx}

\begin{document}
lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum orem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum orem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum orem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum orem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum orem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum orem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum orem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum orem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum orem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum orem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum orem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum orem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum.

\begin{figure}[h] 
    \centering
    \includegraphics[scale=0.35]{./fig1}
    \caption{This is a figure caption.}
    \label{fig:figure1}
\end{figure} 

after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure after figure 

\end{document}

紧接着图表的文本填满了第 1 页的剩余部分,但从新行开始。是否可以将图表后的文本与图表前的文本放在同一行上?

感谢您的帮助!

在此处输入图片描述 在此处输入图片描述

答案1

不是图形插入了换行符(实际上是段落分隔符),而是您在源文件中明确添加了它,空白行强制段落分隔符,如果您删除图形,您会看到相同的内容。

只需删除空白行即可。

几乎总是最好使用[htp]而不是[h]通常 LaTeX 警告[h]并将其更改为[ht]以便可以设置图形。

答案2

举例来说,大卫·卡莱尔的回答

\documentclass[11pt, oneside, letterpaper]{book}
\usepackage{graphicx}
\begin{document}
lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem 
ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum 
lorem ipsum lorem ipsum orem ipsum lorem ipsum lorem ipsum 
lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum 
lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum 
lorem ipsum orem ipsum lorem ipsum lorem ipsum lorem ipsum 
lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum 
lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum orem 
ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum 
lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem 
ipsum lorem ipsum lorem ipsum orem ipsum lorem ipsum lorem ipsum 
lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum 
lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum 
lorem ipsum orem ipsum lorem ipsum lorem ipsum lorem ipsum 
lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum 
lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum orem 
\begin{figure}[h]
    \centering
    \includegraphics[scale=0.35]{example-image-a}
    \caption{This is a figure caption.}
    \label{fig:figure1}
\end{figure}
after figure after figure after 
figure after figure after figure after figure 
after figure after figure after figure after figure after 
figure after figure after figure after figure after 
figure after figure after figure after figure after 
figure after figure after figure after figure after figure after 
figure after figure after figure after figure after figure after 
\end{document}

在此处输入图片描述

答案3

如果您在放置图像时需要精确度,我建议您不要使用浮动。浮动允许图像在文本周围浮动,以根据 TeX 的编程惩罚提供理想的位置。整个目的是尝试让它们显示在页面的顶部、中间或底部,具体取决于排版时文本的布局方式。

以下是自定义图形环境的示例,它不能用作浮动(尽管标题类型为浮动)。我只是劫持了现有的标题代码,该代码旨在在浮动环境中工作。

\documentclass{article}
\usepackage{xparse}% for LaTeX3's \NewDocumentEnvironment
\usepackage{graphicx}% for \includegraphics

\makeatletter
\NewDocumentEnvironment{myfigure}{ O{} m }
  {%
  \def\@captype{figure}% see source2e for using caption outside of float
  \includegraphics[#1]{#2}%
  }
  {}
\makeatother

\begin{document}

 Before before before
\begin{myfigure}[width=.3\textwidth]{Router_Diagram.pdf}
%\caption{Here is a caption.}
\end{myfigure} 
After after after

Before before before
\begin{myfigure}[width=.3\textwidth]{Router_Diagram.pdf}
\caption{Here is a caption.} % note that this adds a new line, but could be remedied by boxing the whole thing in the environment
\end{myfigure} 
After after after


\end{document}

在此处输入图片描述

相关内容