Pandoc Markdown转换为PDF图片位置

39
使用pandoc将Markdown文档转换为PDF时,图像不会放置在我在源代码中放置它们的同一位置。我认为这是由于通过LaTeX进行转换所致,但我不确定如何在Markdown源中解决这个问题。
如果我在源代码中使用一个占位符图像,并且有几段示例文本来策略性地放置图像,它会变得太大,无法适应我放置它的位置上的页面,因此LaTeX布局引擎会把它放在下一页上。然而,我希望这种情况不会发生,因为这意味着图像不在我期望的位置,也更难以引用。
如果需要,我可以提供一个示例,但是需要相当广泛的源文本才能填充整个页面。
3个回答

48

你是否尝试过禁用 implicit_figures,例如:

pandoc -f markdown-implicit_figures -t pdf myfile.md

为了解决大小问题,您可以尝试使用属性在markdown文件中固定大小。类似下面这样的东西就可以解决问题:
![Caption text](/path/to/image){ width=50% }

2
-f markdown-implicit_figures 对我有效。但是似乎 { width=50% } 没有效果。 - Gelin Luo
2
如果您在“)”和“{”之间删除空格符,则“{ width=50% }”会生效。 - Stanislav Pankevich
这个答案似乎解决了问题,但是如果去掉+implicit_figures+,那么图片就不会自动带有图形编号的标题。 - p.matsinopoulos

26
尽管布鲁诺(Bruno)强制将图形放置到指定位置的解决方案有效,但也会移除.md文件中指定的标题,从生成的.pdf文件中。

为了防止图形浮动但保留标题:

1. 创建一个包含以下内容的.tex文件:

\usepackage{float}
\let\origfigure\figure
\let\endorigfigure\endfigure
\renewenvironment{figure}[1][2] {
    \expandafter\origfigure\expandafter[H]
} {
    \endorigfigure
}

这将防止LaTeX浮动插图。

2. 调整pandoc调用

假设您已经将新创建的.tex文件命名为disable_float.tex,只需在pandoc调用中添加-H disable_float.tex

pandoc -H disable_float.tex input.md -o output.pdf

感谢这个SO上的回答以及在github上的这条评论


被接受的答案对我没用,但这种方法解决了我的问题。 - maxywb
我也发现这是唯一对我有效的方法,但由于我以编程方式编写了我的例程,所以我必须提供我的 tex 文件的绝对路径。 - Thom Ives

4
一个与@apitsch的答案相同的替代方案:在文本中调用点处放置图像(虽然不完全“嵌入”到文本中),并附带其标题。
做这个:
  • In your pandoc --include-in-header template (disable_float.tex in @apitsch's answer) use, instead

    % Override default figure placement To be within the flow of the text rather
    % than on it's own page.
    \usepackage{float}
    \makeatletter
    \def\fps@figure{H} 
    \makeatother
    

    For other default positioning schemes see: https://www.overleaf.com/learn/latex/Inserting_Images#Positioning. E.g. in place of H you could specify t.

  • In your markdown source you'll probably also want to specify a height attribute. In the latex context this is height of \textheight (almost the height of the page).

    ![Caption for my image](MyImage.jpg){height=55%}
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接