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.

Friends, I have to put some images in the marginal notes of a text. Unfortunately, I have some issues with the image alignment. Consider this minimum example:

\documentclass[oneside]{book}
\usepackage[T1]{fontenc}

\parskip 7.2pt

\usepackage{graphicx}
\usepackage{bookman}
\usepackage{lipsum}

\DeclareRobustCommand*\standup{%
\begin{flushright}
\includegraphics[scale=.15]{standup.pdf}%
\end{flushright}
}%

\begin{document}

\reversemarginpar

\noindent Hi there, this is my first line. No real meaning here.

\noindent\marginpar{\standup}Hello, this is a short line.

\noindent\lipsum[4]

\noindent\marginpar{\standup}This is probably another long line to be broken into two lines. Let's see if it happens again.

\noindent\lipsum[4]

\noindent\marginpar{\standup}There we go, this line is supposed to be broken into three lines, so I need to add lots of text here in order to achieve that. Let's see if it happens now.

\noindent\lipsum[4]

\noindent\marginpar{\standup}Now I want the text to be broken into four or more lines. How was your day? Mine was good, despite the fact these images don't want to be my friends. So how about now? Now I want the text to be broken into four or more lines.

\end{document}

The standup.pdf file (which is the proper image) can be downloaded here. The output is as follows:

My text 1

The images are aligned with the baseline of the first line. I'd like to achieve the following alignment rule:


  1. If the paragraph has one line, align the center of the image with the center of that line.
  2. If the paragraph has two lines, align the center of the image in half the height of those two lines.
  3. If the paragraph has three or more lines, align the center of the image with the center of the second line.

I hope this image can visually clarify these rules:

My text 2

I tried to manually hack their position using \vspace, but I had no success.

I wonder if someone could enlight me on this issue. I'm open to all ideas, this code was my first attempt. I'm also aware that these rules might be confusing or annoying to be defined, so even a manual adjustment is welcome. =)

share|improve this question
    
This is probably not what you want, but I've done something similar with these lightandmatter.com/books.html illustrated textbooks. The latex code and scripts for doing it are here repo.or.cz/w/light-and-matter.git . The reason I say it's probably not what you want is that it's a big, complex system. –  Ben Crowell Jul 25 '11 at 18:10
    
Thanks @Ben, I'll take a look. =) –  Paulo Cereda Jul 25 '11 at 18:22
add comment

2 Answers 2

up vote 4 down vote accepted

Here is a possibility:

\documentclass[a4paper]{article}
\usepackage[demo]{graphicx}
\usepackage{environ}

\makeatletter
\renewenvironment{fitem}[1]
  {\par\def\whichimage{#1}\Collect@Body\process@fitem}{\par}
\def\process@fitem#1{%
  \setbox0=\vbox{#1\par\xdef\nlines{\the\prevgraf}}%
  \begingroup\clubpenalties=4 10000 10000 10000 0
    \ifcase\nlines\or % no empty paragraphs
      \placefigure{0}\or\placefigure{.5}\else\placefigure{1}\fi
    #1\par\endgroup}
\def\placefigure#1{\leavevmode\smash{\makebox[0pt][r]{%
  \vbox{\hbox{\pauloimage{\whichimage}}
        \vskip-7.2pt % half the height of the images
        \vskip.5ex % compensation for centering at a line
        \vskip-#1\baselineskip % shift down
        }\space}}}
\makeatother


\def\pauloimage#1{\includegraphics[width=14.4pt,height=14.4pt]{try}}

\usepackage{lipsum}

\parskip=7.2pt \parindent=0pt

\begin{document}
\lipsum[2]

\begin{fitem}{stand}
This is a one line paragraph.
\end{fitem}

\begin{fitem}{sit}
This should be a two line paragraph. This is probably another long line to be broken into 
two lines. Let's see if it happens again.
\end{fitem}

\begin{fitem}{kneel}
\lipsum*[2]
\end{fitem}
\end{document}

Of course you have to define correctly \pauloimage. Page breaks are allowed only after the third line.

The paragraph is typeset twice: the first time for getting the number of lines. I've used \prevgraf assuming that it's a plain paragraph. If it's not, then something else must be devised.

share|improve this answer
    
Absolutely fantastic! And best of all, I understood the code! =) Thanks a million for this elegant answer! –  Paulo Cereda Jul 26 '11 at 11:28
add comment

You can measure the height of a paragraph by setting it in a \vbox, so you could design a macro with two arguments: the paragraph and the image. If you declared a new box \para, and then \setbox\para=\vbox{#1} you can then test \ht\para against the current \baselineskip and see how many lines it took. Then reset the paragraph and use \marginpar with a suitable \raisebox on the \includegraphics.

share|improve this answer
    
Thanks @Peter, I'm gonna take a look on this idea, sounds promising. =) –  Paulo Cereda Jul 26 '11 at 11:20
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.