【问题标题】:Convert HTML img tags into figures with caption in LaTeX在 LaTeX 中将 HTML img 标签转换为带有标题的图形
【发布时间】:2012-12-10 11:47:06
【问题描述】:

我正在编写一个 python 脚本来解析 Wordpress Export XML (wp xml) 的内容以生成 LaTex 文档。到目前为止,wp xml 是通过lxml.etree 解析的,代码生成了一个新的 xml 树,由texml 处理,然后生成 tex 文件。

目前我提取每个帖子以及某些元数据(标题、发布日期、标签、内容)。元数据没有问题,但内容部分有点问题。在 wp xml 中,内容作为 CDATA 结构包含在纯 HTML/Wordpress 标记中。要将其转换为乳胶,我选择pandoc 来解析内容。 TeXml 支持内联 LaTeX,因此内容作为纯 LaTeX 添加到树中。

我决定在这种情况下使用 pandoc,因为它已经很好地转换了大多数 html 标签(astrongem...),我唯一的问题是它如何处理图像。

我使用子进程与 pandoc 交互:

args = ['pandoc', '-f', 'html', '-t', 'latex']
p = Popen(args, stdout=PIPE, stdin=PIPE, stderr=PIPE)
tex_result = p.communicate(input=(my_html_string).encode('utf-8'))[0]

示例帖子可能如下所示

<strong>Lorem ipsum dolor</strong>  sit amet, consectetur adipiscing elit.

<a href="http://link_to_source_image.jpg"><img class="alignnone size-medium wp-image-id" title="Title_text" src="http://link_to_scaled_down_version.jpg" alt="Some alt text" width="262" height="300" /></a>

Nam nulla ante, vestibulum a euismod sed, accumsan at magna. Cras non augue risus, vitae gravida quam.

我需要带有嵌入标题的图片,例如

\begin{figure}
\includegraphics{link_to_image.jpg}
\label{fig:some_label}
\caption{Some alt text}
\end{figure}

pandoc 似乎将 html img 标签转换为简单的内联图像,丢弃任何标题或替代文本。

\href{http://link\_to\_source\_image.jpg}{\includegraphics{http://link_to_scaled_down_version.jpg}}

我确实查看了源代码,看起来 img 仅被视为内联元素。 (pandoc parsing function)。我不知道 Haskell,所以这就是我的目标。

如果你将 html 转换为 markdown,它会保留 alt 和 title,结果类似于

![Some alt text](http://link_to_scaled_down_version.jpg "Title_text")

使用 Markdown,您可以在生成的 Latex 文档中包含内联图像或图形。如果将此降价转换为乳胶,则结果为

\begin{figure}[htbp]
\centering
\includegraphics{http://link_to_scaled_down_version.jpg}
\caption{Some alt text}
\end{figure}

第一个 pandoc 似乎是解析内容的简单解决方案,但我有点卡住,因为 pandoc 也不支持 html 中的内联乳胶,所以我可以首先通过 pandoc 处理所有图像和其余图​​像。

你们知道如何(更好地)处理 html 中的 img 标记以嵌入到带有标题的乳胶中的图形环境中吗?

【问题讨论】:

    标签: python xml latex html-parsing pandoc


    【解决方案1】:

    Pandoc 将包含图像的段落特别视为带有标题的图像。这些将变成带有标题的 LaTeX 图形。因此:

    % pandoc -f html -t latex
    <p><img src="myimg.jpg" alt="my text" title="my title"/></p>
    ^D
    \begin{figure}[htbp]
    \centering
    \includegraphics{myimg.jpg}
    \caption{my text}
    \end{figure}
    

    这可能会对你有所帮助。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 2013-10-28
    • 1970-01-01
    • 2016-04-21
    • 2014-03-13
    • 1970-01-01
    相关资源
    最近更新 更多