【问题标题】:How to set the font size for code blocks in pandoc markdown?如何在 pandoc markdown 中设置代码块的字体大小?
【发布时间】:2015-01-16 00:43:48
【问题描述】:

这个小例子:

An example code snippet:

~~~{.cpp}
class A 
{
    public:
        static void f1 () {}; 

        virtual void f2 () = override; 
};
~~~

可用于生成 PDF 输出:

pandoc -o block-code.pdf block-code.txt

导致

代码和文本的字体大小相等。如何更改 pdf (LaTex) pandoc 输出的代码 sn-ps 的字体大小?

【问题讨论】:

    标签: latex pandoc


    【解决方案1】:

    你可以简单地在代码sn-p开始前加上\small,在后面加上\normalsize(恢复正常)。

    您还可以添加其他类似的命令。例如,如果您的文档是双倍行距的,您可以在代码 sn-p 之前添加\singlespace,在之后添加\doublespacing

    为此,您需要在文档开头的 yaml 中添加以下内容:

    ---
    header-includes:
        - \usepackage{setspace}
    ---
    

    【讨论】:

      【解决方案2】:

      我通过将几个 LaTeX sn-ps 写入我保留的额外文件中为我解决了这个问题:

       cat make-code-footnotesize.tex
      
         \renewenvironment{Shaded} {\begin{snugshade}\footnotesize} {\end{snugshade}}
      

      我有适用于所有不同尺寸的 sn-ps:hugeLARGELargelargenormalsizesmallfootnotesizescriptsizetiny

      要在运行pandoc 时应用它们,只需在-H 参数中包含相应的LaTeX sn-p:

       pandoc -o block-code.pdf block-code.txt \
          -H make-code-scriptsize.tex --highlight-style=espresso
      

      结果:

      注意,它控制 PDF 中 所有 代码块的字体大小。它不允许您从块到块改变大小。当然,它也不适用于 HTML、ODT、EPUB 或其他输出——仅适用于 LaTeX 和 PDF 输出。

      【讨论】:

      • 可以将renewenvironment 添加到 pandoc Latex 模板中吗?我试过添加它,在添加 \usepackage{framed} 后,我收到一条错误消息 ! LaTeX Error: Undefined color shadecolor'`。
      • @tmaric:Shaded 环境只有在您使用语法高亮时才会激活。
      • 每当我尝试一种非浓缩咖啡的风格时,我都会得到相同的Package xcolor Error: Undefined color 'shadecolor'.。我可以通过将\colorlet{shadecolor}{white} 添加到我的模板来定义它。
      • 这是要走的路。但它仅适用于具有特殊背景的突出显示样式。见这里:hackage.haskell.org/package/pandoc-1.9.4/docs/… 否则你必须省略 snugshade 环境的开始和结束:\renewenvironment{Shaded} {\footnotesize} {}
      【解决方案3】:

      为此,我为 pandoc https://github.com/chdemko/pandoc-latex-fontsize 开发了一个过滤器:

      使用pip 安装此过滤器:

      $ pip install pandoc-latex-fontsize
      

      添加示例

      ---
      pandoc-latex-fontsize:
        - classes: [c, listing]
          size: footnotesize
      ---
      

      到您的元数据块并指定您希望大小为footnotesize 的列表:

      ~~~{.c .listing}
      int main(void) {
          return 0;
      }
      ~~~
      

      然后运行 ​​pandoc

      $ pandoc --filter pandoc-latex-fontsize ...
      

      【讨论】:

        【解决方案4】:

        在 pandoc 1.16.0.2 上,我确实遇到了同样的问题,但之前的答案没有解决。

        当使用此版本的默认代码突出显示并使用投影仪 (-t beamer) 导出它时,我得到了以下生成的输出(用于输出 jmeter 命令):

        \begin{Shaded}
        \begin{Highlighting}[]
        \KeywordTok{./jmeter.sh} \NormalTok{-q prod.properties -p jmeter.properties  -n -t mytest.jmx -l mylog.log}
        \end{Highlighting}
        \end{Shaded}
        

        通过使用grep -r "Highlighting" *直接在pandoc代码中搜索,我找到了以下代码:

        \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
        

        我将其替换为以下内容,以便在我的自定义 pandoc 模板中使用小字体(请参阅 pandoc -D beamer):

        \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\},fontsize=\tiny}
        

        现在我通过运行以下命令获得了更小的字体大小:

        pandoc -t beamer input.md -o output.pdf --template beamer.template
        

        【讨论】:

          【解决方案5】:

          我将 pandoc 与 --listings 选项和自定义 eisvogel.latex 模板一起使用。

          • 将原eisvogel-template下载到新文件eisvogel_custom.latex
          • 打开文件,找到basicstyle = \color{listing-text-color}\small\ttfamily{}\linespread{1.15},这一行
          • \small 更改为\footnotesize,或\tiny
          • 保存文件

          现在使用以下选项运行 pandoc:

          pandoc --pdf-engine=xelatex --template=eisvogel_custom --listings -o block-code.pdf block-code.txt
          

          要禁用行号,请在block-code.txt 的顶部添加以下行

          ---
          listings-disable-line-numbers: true
          ...
          
          

          所有选项都可以在这里找到:https://github.com/Wandmalfarbe/pandoc-latex-template#custom-template-variables

          使用 docker-image dalibo/pandocker:stable 使用 pandoc 2.6 进行测试

          【讨论】:

            猜你喜欢
            • 2015-06-16
            • 1970-01-01
            • 2014-07-11
            • 1970-01-01
            • 1970-01-01
            • 2016-10-20
            • 1970-01-01
            • 2018-09-17
            • 1970-01-01
            相关资源
            最近更新 更多