根据特定段落高度规则对齐边注中的图像

根据特定段落高度规则对齐边注中的图像

朋友们,我必须在文本的边注中放置一些图像。不幸的是,我在图像对齐方面遇到了一些问题。请考虑这个最小示例:

\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}

站立.pdf文件(正确的图像)可以下载这里。输出如下:

我的文字 1

图像与第一行的基线对齐。我想实现以下对齐规则:


  1. 如果段落只有一行,则将图像的中心与该行的中心对齐。
  2. 如果段落有两行,则将图像的中心对齐到这两行高度的一半。
  3. 如果段落有三行或更多行,则将图像的中心与第二行的中心对齐。

我希望这张图片能够直观地阐明这些规则:

我的文字2

我尝试使用手动破解他们的位置\vspace,但没有成功。

我想知道是否有人可以就这个问题给我一些启发。我愿意接受所有想法,这段代码是我的第一次尝试。我也知道这些规则可能会令人困惑或令人讨厌,所以即使是手动调整也是受欢迎的。=)

答案1

有以下可能性:

\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}

当然你必须正确定义\pauloimage。只有在第三行之后才允许分页。

该段落排版两次:第一次是为了获取行数。我曾经\prevgraf假设它是一个普通段落。如果不是,那么必须想出其他办法。

答案2

您可以通过在 中设置来测量段落的高度\vbox,因此您可以设计一个包含两个参数的宏:段落和图像。如果您声明了一个新的框\para,然后\setbox\para=\vbox{#1}您可以\ht\para针对当前框进行测试\baselineskip并查看它占用了多少行。然后重置段落并在 上使用\marginpar合适的。\raisebox\includegraphics

相关内容