使用Pandoc将Markdown转换为PDF时使用CSS [英] Using CSS when converting Markdown to PDF with Pandoc

查看:188
本文介绍了使用Pandoc将Markdown转换为PDF时使用CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在OS X上试用Pandoc,到目前为止,结果令人印象深刻.但是,一个阻碍性的问题是使CSS样式可用于内联代码示例.我正在从Markdown转换为PDF.

I'm trying out Pandoc on OS X, and results thus far are impressive. One blocking problem, however, is getting CSS styles to work on inline code samples. I'm converting from Markdown to PDF.

我的源代码中有此字符串:

I have this string in my source:

* Create a simple HTML document (<span class="filename">simple.html</span>) and load it into the browser via the file system

我也尝试过:

* Create a simple HTML document (`simple.html`{.filename}) and load it into the browser via the file system

在每种情况下,我都想将类文件名"应用于所包含的文本,但是它似乎对输出没有任何作用.但是手册中说:

I'd like to apply the class "filename" to the enclosed text in each case, but it doesn't seem to do anything to the output. However the manual says:

某些输出格式可以使用此信息来突出显示语法.当前,使用此信息的唯一输出格式是HTML和LaTeX.

Some output formats can use this information to do syntax highlighting. Currently, the only output formats that uses this information are HTML and LaTeX.

这是我的命令:

pandoc \
    --output ./output.pdf \
    --css source/styles.css \
    source/en/docs/input.md

我正在转换为PDF,这是Pandoc在内部由LaTeX编写的.我可以让它工作吗?或者,我可以使用通过LaTeX命令定义的样式吗? -不必是CSS.但是,它必须是样式系统-每次都不能更改斜体/字体/颜色属性.

I'm converting to PDF, which is written by LaTeX by Pandoc internally. Can I get this to work? Or, can I use a style defined using a LaTeX command? - it doesn't have to be CSS. However, it must be a style system - it's not workable to change italic/font/colour attributes on each occasion.

我尝试将输出临时发送到HTML,在这种情况下,样式是直接从特定样式资产中导入的.因此,至少对于一种输出格式,我的样式表规范和跨度标记是正确的.

I've tried sending output temporarily to HTML, and in that situation the styles are imported directly from the specific style asset. So, my stylesheet specification and span markup is correct, at least for one output format.

一些事后思考:

  • 解决方案不必是Pandoc或Markdown.但是,它确实需要是一种可基于文本的标记语言,可以可靠地转换为PDF,因为我想将文档文件存储在Git上,以方便分叉和合并.我不喜欢HTML,因为它很冗长,并且转换它的引擎并不是那么好(尽管,当然,我的格式要求不高)
  • pandoc的HTML输出很好,因此,如果我能找到可靠地将(简单的)HTML/CSS转换为PDF的东西,那就很好了.当然,Pandoc应该能够做到这一点,但是不会呈现内联样式(用于代码片段的背景色).这可能很麻烦,因为我必须重新引入分页符之类的内容,这在HTML到PDF转换器中可能并不简单.

推荐答案

我一点都不了解LaTeX,但是已经使用

I don't know LaTeX at all, but have hacked this solution using this helpful manual. First, create a style:

\definecolor{silver}{RGB}{230,230,230}

\newcommand{\inlinecodeblock}[1]{
    \colorbox{silver}{
        \texttt{#1}
    }
}

这是使用方法:

Some \inlinecodeblock{inline code}, and some widgets, go here

这将创建具有背景颜色和等宽字体的样式.根据我的喜好,边距和填充有些大,但这是一个非常有用的开始.看起来是这样的:

This creates a style with a background colour and a monospaced font. The margin and padding are a bit large for my preferences, but it's a very useable start. Here's what it looks like:

缺点是,如果我希望输出为支持适当样式的格式(例如HTML),则会丢失这些样式.另外,我的解决方案仅适用于LaTeX/PDF.因此,如果您可以解决这些问题,请添加更好的答案!

The disadvantage is that if I wish to output to a format that supports styles proper (such as HTML) then these are lost. Also, my solution only works with LaTeX/PDF. Thus, if you can fix these issues, please add a better answer!

附录:我有一个更好的方法,那就是:

Addendum: I have a better approach, which is thus:

\newcommand{\inlinecodeblock}[1]{
    \fboxsep 1pt
    \fboxrule 0pt
    \colorbox{silver}{\strut{\texttt{#1}}}
}

这避免了过多的水平填充问题-我认为是colorbox参数中的换行符造成的.我在strut中添加了内容,无论文本是否具有下标,它都会使突出显示的高度保持不变.

This avoids the problem of excess horizontal padding - I think it was the line break in the colorbox parameter that did it. I've added in strut, which keeps highlights the same height regardless of whether the text has descenders.

但这并不完美-盒子外面还有太多的水平边距,盒子后面的逗号仍然会孤立到下一行.我可能会放弃LaTeX,从Pandoc渲染为HTML,然后使用wkhtmltopdf渲染最终文档.

It's not perfect though - there's still too much horizontal margin outside the box, and a comma after a box will still orphan onto the next line. I may give up with LaTeX, and render to HTML from Pandoc, and then use wkhtmltopdf to render the final document.

这篇关于使用Pandoc将Markdown转换为PDF时使用CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆