5

I am trying to place an image on the right of some individual lines of text that do not stretch over the entire \textwidth. The image will go higher than the first line and I have to be able to position the image very precisely. My idea was:

\documentclass{article} 
\usepackage{graphicx}
\textwidth=5cm
\begin{document}
\setlength{\parindent}{0pt}
A\\
B\\[-2cm]
C\hfill\includegraphics[height=2cm]{testimg}\\
D
\end{document}

which produces

What I don't like is that B and C are not on the same line, which means that in the the space between B and D is larger than the space between B and A (C will not be in the final document, it is just inserted for illustration).

Any ideas would be appreciated.

2 Answers 2

3

You should put the image in a vertical version of \rlap so that officially it doesn't take any vertical space. You can do this like this:

\documentclass{article} 
\usepackage{graphicx}
\textwidth=5cm
\begin{document}
\setlength{\parindent}{0pt}
A\\
B\hspace{4cm}\hbox to 0pt{\vbox to 0pt{\vss\rule{5cm}{2cm}}\hss}\\
D
\end{document}

(Here \rule is a demo replacement of the \includegraphics)

This also makes the image officially 0pt wide and you can position it using the value of \hspace.

However, you could control the position even more exact if you put both the text and the image into a picture environment like e.g. tikzpicture.

5
  • It's dangerous to use a box like this because you can erase something above the picture. You can try \hfill\rule{5cm}{2cm} before A for example. Mar 15, 2011 at 16:10
  • @Altermundus: Sure, but the original code with \\[-2cm] isn't better. I wouldn't do this in general but in the OP's case if you want it this way and know that there is nothing covered by the image you can use it. Mar 15, 2011 at 16:14
  • Thanks for your answer! I have been trying to integrate it into my document. Two things I struggle with: (a) Since the hbox is 0pt, I can't use \hfill to align the image on the right page border. (b) In this approach, is there a way to control the vertical position of the image also? I realized that I might want the image bottom to be between two lines (Sorry, this last requirement was not mentioned in my question).
    – st12
    Mar 15, 2011 at 16:38
  • 1
    @Frank: You don't need to make the \hbox 0pt wide. Just remove the ` to 0pt` after it. Also you can try \raisebox{<value>}{<content>} to adjust the vertical positioning. Mar 15, 2011 at 16:47
  • \raisebox worked great! I am not having luck with \hfill \hbox {\vbox to 0pt {..., however: The image is placed right next to the letters...
    – st12
    Mar 15, 2011 at 16:59
1

Possibly two minipages (remark: the use of \\ inside a document should be avoided):

\documentclass{article} 
\usepackage{graphicx}
\begin{document}
\setlength{\parindent}{0pt}
\begin{minipage}[c]{.45\textwidth}
A

B

C
\end{minipage}\hfill
\begin{minipage}[b]{.45\textwidth}
\includegraphics[height=2cm]{testimg}
\end{minipage} 

\end{document}
4
  • why do you say that the use of \\ should be avoided? Mar 15, 2011 at 16:23
  • @Gonzalo Outside a table, a \\ specifies the end of a paragraph, and the start of a new one, which is not indented. It's a linebreaking. It's useful in some cases but generally you stop TeX to do is natural work. This is ok in a title but I prefer to avoid it to format text lines. But perhaps I'm wrong .. :( Mar 15, 2011 at 17:19
  • but \\ doesn't end a paragraph; \\ starts a new line inside an existing paragraph! On the other hand, I don't understand what you mean by "you stop TeX to do its natural work". The command \\ properly used is really very handy. Mar 15, 2011 at 17:33
  • @Gonzalo You are right I don't use the good verb but my english is not good.\` forces a linebreak somewhere in a block of text, and forces LaTeX to start a new line, but not a new paragraph. It is not to end` but to break the line and a paragraph. It's not natural inside a paragraph to break a line like that, but perhaps it's only a french approach and it's possible with another language. I understand it's useful in some specific cases and this command can be used properly. But not too much ... and not everywhere Mar 15, 2011 at 18:18

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.