Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

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.

share|improve this answer
    
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. –  Alain Matthes Mar 15 '11 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. –  Martin Scharrer Mar 15 '11 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). –  Frank Seifert Mar 15 '11 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. –  Martin Scharrer Mar 15 '11 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... –  Frank Seifert Mar 15 '11 at 16:59
add comment

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}
share|improve this answer
    
why do you say that the use of \\ should be avoided? –  Gonzalo Medina Mar 15 '11 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 .. :( –  Alain Matthes Mar 15 '11 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. –  Gonzalo Medina Mar 15 '11 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 –  Alain Matthes Mar 15 '11 at 18:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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