0

In many Latex docs, I see people do:

This is an example paragraph... blah...blah...
%
\begin{figure}
    ...
\end{figure}
%
and then blah...blah...

I'm confused, what is the purpose of % before and after the figure?

What is the difference with

This is an example paragraph... blah...blah...
\begin{figure}
    ...
\end{figure}
and then blah...blah...

2 Answers 2

2

The comments indicate that those empty lines are ignored by LaTeX, so "and then..." are not put to a different paragraph.

See the difference:

% CODE 1
\documentclass{article}
\usepackage{graphicx}
\begin{document}
This is an example paragraph... blah...blah... is an example paragraph... blah...blah...
is an example paragraph... blah...blah... is an example paragraph... blah...blah...
is an example paragraph... blah...blah... is an example paragraph... blah...blah...

\begin{figure}
  \includegraphics{example-image-duck}
\end{figure}

and is an example paragraph... blah...blah... is an example paragraph... blah...blah...
is an example paragraph... blah...blah... is an example paragraph... blah...blah...
is an example paragraph... blah...blah... is an example paragraph... blah...blah...
\end{document}

enter image description here

% CODE 2
\documentclass{article}
\usepackage{graphicx}
\begin{document}
This is an example paragraph... blah...blah... is an example paragraph... blah...blah...
is an example paragraph... blah...blah... is an example paragraph... blah...blah...
is an example paragraph... blah...blah... is an example paragraph... blah...blah...
%
\begin{figure}
  \includegraphics{example-image-duck}
\end{figure}
%
and is an example paragraph... blah...blah... is an example paragraph... blah...blah...
is an example paragraph... blah...blah... is an example paragraph... blah...blah...
is an example paragraph... blah...blah... is an example paragraph... blah...blah...
\end{document}

enter image description here

You can also remove the blank lines to avoid paragraph breaking. The following code and CODE 2 are treated equally by latex, but the ignored empty lines make the code more readable.

% CODE 3
\documentclass{article}
\usepackage{graphicx}
\begin{document}
This is an example paragraph... blah...blah... is an example paragraph... blah...blah...
is an example paragraph... blah...blah... is an example paragraph... blah...blah...
is an example paragraph... blah...blah... is an example paragraph... blah...blah...
\begin{figure}
  \includegraphics{example-image-duck}
\end{figure}
and is an example paragraph... blah...blah... is an example paragraph... blah...blah...
is an example paragraph... blah...blah... is an example paragraph... blah...blah...
is an example paragraph... blah...blah... is an example paragraph... blah...blah...
\end{document}
3
  • well, if so, why don't we write just after \end{figure}\n without using %?
    – null
    Dec 31, 2019 at 8:31
  • @NathanExplosion As I said, it will push the "and..." to a new paragraph.
    – fractal
    Dec 31, 2019 at 8:31
  • Aha, more readable it is^
    – null
    Dec 31, 2019 at 8:34
2

The only purpose of the figure environment is to take its content out of the main document flow, so that it can be inserted later, if you need an image mid-paragraph, you could use \includegraphcs directly in the text. So you could ask the question without the figure there at all.

The following shows four possible markup styles for the sentence

# This is an example paragraph... blah...blah... and then blah...blah...

enter image description here

\documentclass{article}

\begin{document}


1 This is an example paragraph... blah...blah... and then blah...blah...

2 This is an example paragraph... blah...blah...
and then blah...blah...

3 This is an example paragraph... blah...blah...
%
%
and then blah...blah...

4 This is an example paragraph... blah...blah...
%
{}
%
and then blah...blah...


\end{document}

Note that the first three are equivalent, as comments are ignored, and ends of lines are treated as spaces. The fourth is usually a markup error, as it introduces a double word space before and.

You can insert a figure environment into the sentence in any of these styles, the style you show is essentially equivalent to 4, except that unlike {}, the figure environment takes special precautions if it has a space before and after the environment and removes the space before.

I think that 1 is the most natural markup for a sentence so in the (somewhat rare) cases where you need to specify a figure callout mid-sentence I would use

This is an example paragraph... blah...blah...\begin{figure}
    ...
\end{figure} and then blah...blah...

Which highlights its mid-sentence nature.

Normally it is clearer to specify figures between paragraphs, when you can use blank lines around the figure environment markup.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .