4

Consider the following code (no special packages added)

\begin{document}
Text Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text 

\begin{center}
\includegraphics{black_square} and \includegraphics{black_square}
\par\end{center}

Text Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text 
\end{document}

that produces the output enter image description here, where the black squares stand for some image I wand to put in my document.

My questions are:

  1. Is this the right way to add images to a document ? I'm vaguely aware that there are some floating environments available for this, but I want the images at exactly this specific place.

  2. How can I rearrange the black square so that they are horizontally evenly distributed and the "and" is vertically in the middle, i.e. more like this: enter image description here

3

Since \includegraphics places images upon the text baseline, I here use \raisebox to shift the figures down (1/2 of their height - 1/2 the height of a textline). Then, by placing symmetric \hspaces around the word "and", I can achieve a symmetric layout. I use % at the end of the lines to prevent stray spaces from getting introduced.

\documentclass{article}
\usepackage[demo]{graphicx}
\begin{document}
Text Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text 

\begin{center}
\raisebox{\dimexpr-.5\height+.5\ht\strutbox}%
  {\includegraphics[width=1in,height=1in]{black_square}}%
\hspace{.5in}%
and%
\hspace{.5in}%
\raisebox{\dimexpr-.5\height+.5\ht\strutbox}%
  {\includegraphics[width=1in,height=1in]{black_square}}
\par\end{center}

Text Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text 
\end{document}

enter image description here

If you wanted the figures to be exactly spaced between the margin and the middle text, this would suffice, replacing the center environment with the appropriate \hfils, making sure to not indent the "paragraph" on which the figures appear.

\documentclass{article}
\usepackage[demo]{graphicx}
\begin{document}
Text Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text 

\medskip
\noindent
\hfil%
\raisebox{\dimexpr-.5\height+.5\ht\strutbox}%
  {\includegraphics[width=1in,height=1in]{black_square}}%
\hfil%
and%
\hfil%
\raisebox{\dimexpr-.5\height+.5\ht\strutbox}%
  {\includegraphics[width=1in,height=1in]{black_square}}%
\hfil%
\par\medskip

Text Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text 

\medskip
\noindent
\hfil%
\raisebox{\dimexpr-.5\height+.5\ht\strutbox}%
  {\includegraphics[width=1.3in,height=1.3in]{black_square}}%
\hfil%
and%
\hfil%
\raisebox{\dimexpr-.5\height+.5\ht\strutbox}%
  {\includegraphics[width=1.3in,height=1.3in]{black_square}}%
\hfil%
\par\medskip

Text Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text Text Text Text Text Text Text Text Text Text Text Text Text
Text Text 
\end{document}

enter image description here

  • What do you mean by "\includegraphics places images upon the text baseline" ? Why exactly is the space between the text above and below the picture the same if you raise it ? – l7ll7 May 21 '14 at 13:07
  • I'm also confused by the \medskip \noindent \hfil% before the picture and the \medskip after it from your second code sample. Is this just a replacement for \begin{center} and \end{center} ? – l7ll7 May 21 '14 at 13:15
  • @user10324 To your Q1, if you type xyz\includegrphics{}, the bottom of the image will be aligned with the bottom of the x (the baseline). So, one must either raise the text, or lower the figure to achieve your vertical centering goal. I chose to lower the figures. – Steven B. Segletes May 21 '14 at 13:27
  • @user10324 To Q2, the reason I eliminated the center environment is that it plays some havoc with the "intensity", for lack of a better word, of the \hfil macros. One can often use centering, and adjust the fills to some judicious combination of \hfil and \hfill, but I thought it easier to avoid the center environment altogether. The \medskips are just a way to add some vertical gap, which otherwise would have come from the center environment. – Steven B. Segletes May 21 '14 at 13:31
  • @user10324 Back to Q1, \height will contain the height of the \includegraphics image, whereas \ht\strutbox is the height of one line of text. So, mathematically, if you wish to center two objects of those respective heights, the net adjustment has to be 1/2 the difference of those two heights. And that is the value of my adjustment. – Steven B. Segletes May 21 '14 at 13:44
2

Answer with \hfill. But and is not vertically centered.

\documentclass[a4paper]{scrbook}
\usepackage[demo]{graphicx}


\begin{document}

\begin{center}
\includegraphics{black_square} \hfill  and  \hfill \includegraphics{black_square}
\par
\end{center}

\end{document}

Answer with 'subcaption' Now and is vertically centered.

\documentclass{scrbook}
\usepackage[demo]{graphicx}
\usepackage{subcaption}

\begin{document}
text text text text text text text text text text text text text text text
\begin{figure}[htbp!]
    \centering
      \begin{subfigure}{0.31\textwidth}
        \includegraphics[width=\textwidth]{image1}
      \end{subfigure}
\hfill and \hfill
      \begin{subfigure}{0.31\textwidth}
        \includegraphics[width=\textwidth]{image1}
      \end{subfigure}
\end{figure}
some text text text text text text text text text text text text text text text

\end{document}

enter image description here

  • The second example looks good. But two issues remain: 1) In the second example you provided, I think you first have to add \usepackage{caption} otherwise it throws an error. 2) How can I add text before the figure ? Placing text before \begin{figure} still moves it after the figure when compiled. – l7ll7 May 21 '14 at 10:03
  • writelatex.com shows no error. Now result ok? – sandu May 21 '14 at 10:13
  • Yes, though I accepted the other answer because it was slightly more detailed and also did without the subcaption package. Thank you nonetheless for your effort, I upvoted you. – l7ll7 May 21 '14 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.