图形 - 动态图像放置

图形 - 动态图像放置

这是我在此帖子的后续内容:图形 - 多列环境中页面外的图像

使用 C#、XML、SQL 和 LaTeX,我成功创建了一个系统,该系统可以检索数据库信息并填写 LaTeX 命令,从而自动创建排版精美的目录并发送给客户。该系统的优点在于它完全无需人工​​干预,编写完所有命令后无需修改 LaTeX 代码。

目录使用multicol环境,因为目录中的每个部分可能具有不同的列数。我发现multicol使用环境中的文本换行图像效果最好picins我之前的帖子使用该needspace包自动拆分列以确保图像不会超出页面。目前的问题是,该needspace包在目录中留下了不必要的空白,这不仅会降低美观度,还会累积并向目录中添加额外的页面。

以下代码:

\documentclass{book}
\usepackage{multicol}
\setlength{\columnsep}{0.375 in}
\usepackage{picins}
\usepackage{graphicx}
\usepackage[usenames,dvipsnames]{color}
\usepackage[includeheadfoot, top=.25in, bottom=.25in, left=.5in, right=.5in]{geometry}
\usepackage{needspace}    
\usepackage[english]{babel}
\usepackage{blindtext}

\newcommand{\ItemImage}[5]%
{\pichskip{#5 in}\parpic[#2][#3]{\includegraphics[width=#4 in]{#1.png}}}

\newcommand{\ItemDescription}[1]%
{\noindent\color{black}#1\newline}

\begin{document}
\begin{multicols*}{2}
Book Title Number One\\
This is the subtitle of the first book\\
First Name, Last Name\\
\ItemImage{book1}{sr}{r}{1.25}{.125}
\ItemDescription{\blindtext[3] I was often using any of the available “lorem ipsum” generators on the web while testing different things in Latex until I discovered that the Latex distribution provides packages generating blind text, which is definitely more convenient. With just a few lines of code}
000-0-00000-000-0,Hb,\$19.95\\
Book Series\\
6 x 9,100,2012\\
\EndRule

\needspace{20\baselineskip}
Book Title Number Two\\
This is the subtitle of the second book\\
First Name,Last Name\\
\ItemImage{book1}{sr}{r}{1.25}{.125}
\ItemDescription{\blindtext}
000-0-00000-000-0,Hb,\$19.95\\
Book Series\\
10 x 12,500,2012\\
\EndRule

\end{multicols*}
\end{document}

创建类似于以下内容的内容:

理想的情况是(该图形不是用 LaTeX 制作的):

因此,在理想情况下,图像不应放在书籍描述之前:

*Put Image Here* This would be the description starting in column 1. This line is in
column 1 also.  This line can't fit in column 1 and has moved to column 2

该图像将被放置在图书描述中:

This would be the description starting in column 1. This line is in column 1.  *Put 
Image Here* This line can't fit in column 1 and has moved to column 2

由于文本是从数据库中自动放置的,因此需要某种方式让 LaTeX 自动找出正确放置图像的位置。首先想到的是浮动,但是在multicolpicins 效果最佳且不浮动的环境中,文本换行是必不可少的。

我的最终问题是:

有没有办法计算一列中剩余的行数,将该值与图像占用的行数进行比较,并将图像动态地放置在文本换到下一列的文本块内?

这是一种相当迂回的提问方式,但我非常愿意接受建议并指明如何实现上述理想情况的正确方向。

答案1

这是一个不太完美的概念。它用于pgfpagesnodes确定是否有足够的空间放置图片。特别感谢垃圾收集器让我意识到这个问题。未解决的问题实在太诱人了。

  • 接下来的描述需要分开。蒂克兹foreach 用作,分隔符,这并不理想。因此我问了一个问题,如何改变它,并感谢埃格尔,它现在使用|(但几乎任何字符都是可能的)。
  • 图片会尽早设置。如果无法在文本中设置,则会将其单独放置在最后。如果这也失败,则会以红色显示错误消息。
  • 由于某种原因,两者皮钦斯包裹图当我尝试在它们后面添加文本时失败了。皮钦斯需要以 结尾\picskip{0},说明的细分不应太短。为了避免将图片推到最后,它们不应太长。我的样句长度为五句话应该没问题(方形图像为 1.25 英寸)。
  • 可能需要多次编译。蒂克兹请记住,图片选项需要两次编译,第二次编译时可能会发生变化。这可能会使后面的图像超出范围,然后需要重新编译,依此类推。
  • 红点只是为了显示图像将被放置在哪里。
  • 由于某些我不知道的原因,所有非图像描述段落中都有可疑空格。
  • 很可能还存在其他问题,我们非常欢迎提出改进意见。

代码

\documentclass{book}
\usepackage{multicol}
\setlength{\columnsep}{0.375 in}
\usepackage{picins}
\usepackage{graphicx}
\usepackage[usenames,dvipsnames]{color}
\usepackage[includeheadfoot, top=.25in, bottom=.25in, left=.5in, right=.5in]{geometry}
\usepackage[english]{babel}
\usepackage{tikzpagenodes}
\usepackage{xifthen}
\usepackage{xparse}
\ExplSyntaxOn

\NewDocumentCommand{\Foreach}{ m O{,} m}
 {
  \__foreach_main:nn { #1 } { #2 } { #3 }
 }

\seq_new:N \l__foreach_arg_seq
\clist_new:N \l__foreach_braced_clist

\cs_new_protected:Npn \__foreach_main:nn #1 #2 #3
 {
  \seq_set_split:Nnn \l__foreach_arg_seq { #2 } { #3 }
  \clist_clear:N \l__foreach_braced_clist
  \seq_map_inline:Nn \l__foreach_arg_seq
   {
    \clist_put_right:Nn \l__foreach_braced_clist { { ##1 } }
   }
  \foreach #1 in~\l__foreach_braced_clist
 }
\ExplSyntaxOff

\parindent0mm

\newcommand{\ItemImage}[5]%
{\pichskip{#5 in}\parpic[#2][#3]{\includegraphics[height=#4 in]{#1.png}}}

\newcommand{\descriptionimage}[6]%
%   #1 image name
% #2
% #3
% #4 image height
% #5
% #6 description in parts: part_1|part_2|...,|part_n
{   \xdef\picturealreadyset{0}%
    \Foreach{\x}[|]{#6}%
    {   \begin{tikzpicture}[overlay, remember picture]%
            \path (current page text area.south);%
            \pgfgetlastxy{\somedummy}{\textbottom}%
            \xdef\bottomofthetext{\textbottom}%             
            \fill[red] (0,0) circle (0.1);%
        \end{tikzpicture}%
        \pgfmathsetmacro{\inchesabovebottom}{-\bottomofthetext/72.27}%
        \pgfmathtruncatemacro{\placepicturenowboolean}{#4 < \inchesabovebottom ? 1 : 0}%
        \ifthenelse{\placepicturenowboolean = 1 \AND \picturealreadyset = 0}%
        {   \ItemImage{#1}{#2}{#3}{#4}{#5}%
            \x%
            \picskip{0}%
            \xdef\picturealreadyset{1}%
        }%
        {\x}%
    }%
    \ifthenelse{\picturealreadyset = 0}%
        {   \begin{tikzpicture}[overlay, remember picture]%
                \path (current page text area.south);%
                \pgfgetlastxy{\somedummy}{\textbottom}%
                \xdef\bottomofthetext{\textbottom}%                         
                \fill[red] (0,0) circle (0.1);
            \end{tikzpicture}%
            \pgfmathsetmacro{\inchesabovebottom}{-\bottomofthetext/72.27}%
            \pgfmathtruncatemacro{\placepicturenowboolean}{#4 < \inchesabovebottom ? 1 : 0}%
            \ifthenelse{\placepicturenowboolean = 1}%
                {   \ItemImage{#1}{#2}{#3}{#4}{#5}\vspace{#4 in}}%
                {   \textcolor{red}{Image not set: didn't fit anywhere. Check dimensions!\\}}%
        }%
        {}% 
}

\newcommand{\sase}[1][2]
{   \foreach \s in {1,...,#1}
    { This is the same old sentence which will be used over and over and over.}
}

\begin{document}

\begin{multicols*}{2}
\textbf{Book Title Number One\\
This is the subtitle of the first book\\
First Name, Last Name}\par
\descriptionimage{book}{sr}{r}{1.25}{.125}{\sase[5]|\sase[5]|\sase[5]|\sase[5]|\sase[5]|\sase[5]}
\par 000-0-00000-000-0,Hb,\$19.95\\
Book Series\\
6 x 9,100,2012\\
\hrule\vspace{0.2in}

\textbf{Book Title Number Two\\
This is the subtitle of the second book\\
First Name,Last Name}\par
\descriptionimage{book}{sr}{r}{1.25}{.125}{\sase[5]|\sase[5]}
\par 000-0-00000-000-0,Hb,\$19.95\\
Book Series\\
10 x 12,500,2012\\
\hrule\vspace{0.2in}
\end{multicols*}

\end{document}

结果

在此处输入图片描述

相关内容