如何在 Pandoc 中将 .md 中的图像转换为 PDF 后删除换行符或 SoftBreak

问题描述 投票:0回答:1

我有一个包含以下内容的 .md 文件。一些图像在图像后有 LineBreak 或 SoftBreak,如下所示:

![sample.PNG](/.attachments/sample.PNG =516x434)  
*Figure: Premises tab* 

有些图像文件在图像后没有LineBreak或SoftBreak:

![sample2.PNG](/.attachments/sample2.PNG =516x434)

我在 lua 脚本中使用图像函数在每个图像后添加换行符:

function Image (img)
  -- remove leading slash from image paths
  img.src = img.src:gsub('^/', '')

  -- find the index and values for the size definition and apply them to the image
  idx, _, width, height  = string.find(img.src, "%%20=([%d]*)x([%d]*)")

  if (idx ~= nil) then
    img.src = string.sub(img.src, 1, idx - 1)
  end
  return {
    pandoc.RawInline('latex', '\\hfill\\break{\\centering'),
    img,
    pandoc.RawInline('latex', '\\par}')
  }
end

当我运行下面的 pandoc 命令时。它会抛出错误并且不会生成 PDF 文件。

pandoc test.md -V geometry:margin=1in -H header.tex -o PDFs\test.pdf --lua-filter pdf-filters.lua --pdf-engine=xelatex -V fontsize=9pt -V colorlinks=true  -V linkcolor=blue

这是运行上述命令时抛出的错误。

Error producing PDF.
! LaTeX Error: There's no line here to end.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...

l.243 \emph

如何在每张图片后生成带有换行符的 PDF 文件。 感谢您的帮助!

pdf lua pandoc xelatex
1个回答
0
投票

我通过检查图像是否是当前段落中的最后一个元素来解决这个问题。 这是lua图像功能:

function Image (img)
  -- remove leading slash from image paths
  img.src = img.src:gsub('^/', '')

  -- find the index and values for the size definition and apply them to the image
  idx, _, _, _  = string.find(img.src, "%%20=([%d]*)x([%d]*)")

  if (idx ~= nil) then
    img.src = string.sub(img.src, 1, idx - 1)
  end

  -- check if the image is the last element in the current paragraph
  local last_in_paragraph = false
  local parent = img
  while (parent and not last_in_paragraph) do
    parent = parent.parent
    if (parent and parent._type == "Para") then
      local last_child = parent.content[#parent.content]
      if (last_child == img) then
        last_in_paragraph = true
      end
    end
  end

  -- add a line break or new paragraph depending on whether the image is the last element in the current paragraph
  if (last_in_paragraph) then
    return {
      pandoc.RawInline('latex', '\\hfill\\break{\\centering'),
      img,
      pandoc.RawInline('latex', '\\par}')
    }
  else
    return {
      pandoc.RawInline('latex', '\\hfill\\break{\\centering'),
      img,
      pandoc.RawInline('latex', '}')
    }
  end
end
© www.soinside.com 2019 - 2024. All rights reserved.