pandoc 模板 $if$ 语句用于 markdown 中的隔离代码片段

pandoc 模板 $if$ 语句用于 markdown 中的隔离代码片段

我想知道是否可以在 Pandoc 的 latex.template 中执行以下操作:

\begin{document}

$if(fencedcode)$
\begin{lstlisting}
$content within fenced code$
\end{lstlisting}
$endif$

\end{document}

$content within fenced code$在 markdown 文件中将标记如下(如Pandoc 用户指南

```bash
    #!/bin/bash
    echo "hello world"
```

也许为了更好地解释我的问题,我想避免在我的 markdown 文件中写类似这样的内容:

#Introduction
Content…

\begin{lstlisting}{bash}

    #!/bin/bash
    echo "hello world"

\end{lstlisting}

more content…

因此,使用 less 可以使 markdown 文件更干净、更易于阅读raw latex,就像这样:

#Introduction
Content…

```bash
    #!/bin/bash
    echo "hello world"
```

more content…

编辑

latex命令\begin{lstlisting}…\end{lstlisting}实际上是文件内的个性化列表命令class.cls。所以我试图实现的是,当source.md使用 pandoc 和相关模板处理文件时,当 Pandoc 看到它fenced code block不是使用常规\begin{listings}命令而是使用个性化命令来处理它\begin{TMcode}…\end{TMcode},该命令定义如下class.cls

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{TMarticle}
\LoadClass{article} % we use a standard article as a base-class

\RequirePackage{listings} % for our code listings
\RequirePackage[usenames, dvipsnames, svgnames, table]{xcolor}
\RequirePackage{mdframed}
\RequirePackage{graphicx}
\RequirePackage{calc}
\RequirePackage{array}
\RequirePackage{tabu}
\RequirePackage{longtable}
\RequirePackage{fontspec}
\RequirePackage{amsthm}
\RequirePackage{thmtools}

% color definitions
\definecolor{TMgreen}{RGB}{14, 191, 48}
\definecolor{TMorange}{RGB}{243, 126, 25}
\definecolor{TMred}{RGB}{230, 6, 85}
\definecolor{TMcodeBackground}{RGB}{224, 224, 224}
\definecolor{TMcodeFrame}{RGB}{109, 108, 109}
\definecolor{TMtableHead}{RGB}{230, 6, 85}
\definecolor{TMtableRowTwo}{RGB}{230, 230, 230}
\definecolor{TMtableRowOne}{RGB}{240, 240, 240}
\definecolor{TMemphasis}{RGB}{165, 32, 23}
\definecolor{TMwarning}{RGB}{250, 175, 52}
\definecolor{TMcritical}{RGB}{229, 0, 72}
\definecolor{TMnormal}{RGB}{54, 160, 220}
\definecolor{TMbulletinBackground}{RGB}{224, 224, 224}
\definecolor{TMtheorem}{RGB}{14, 191, 48}

% font settings
\defaultfontfeatures{Ligatures=TeX}
\setmonofont{Source Code Pro Light}[BoldFont=Source Code Pro]
\setmainfont{Lato Light}[
    Numbers=OldStyle,
    BoldFont=Lato Regular,
    ItalicFont=Lato Light Italic,
    BoldItalicFont=Lato Italic
]
\setsansfont{Lato Light}[
    Numbers=OldStyle,
    BoldFont=Lato Regular,
    ItalicFont=Lato Light Italic,
    BoldItalicFont=Lato Italic
]
\newfontfamily{\titlefont}{Lato Light}[
    Numbers=OldStyle,
    BoldFont=Lato Regular,
    ItalicFont=Lato Light Italic,
    BoldItalicFont=Lato Italic
]

% Font for chapter number
\newfontfamily{\upperNumber}{Lato Light}[
    BoldFont=Lato Regular,
    ItalicFont=Lato Light Italic,
    BoldItalicFont=Lato Italic
]

% we now start creating the TM-styled code listing environments
% the lstlisting environment is wrapped in an MD-frame
% first of all we need to define the mdframestyle that
% takes care of the background and the left bar

\mdfdefinestyle{TMstyleCode}{
            skipabove=4mm,
            skipbelow=0mm,
            %remove borders
            rightline=false,
            topline=false,
            bottomline=false,
            linewidth=1mm,
            %margins
            innertopmargin=2mm,
            innerleftmargin=0mm,
            innerbottommargin=0mm,
            innerrightmargin=10pt,
            linecolor=TMcodeFrame,
            backgroundcolor=TMcodeBackground
}

\lstdefinestyle{TMstyle}{
    showstringspaces=false,
    numbers=left,
    numbersep=7mm,
    numberstyle=\color{Black},
    stepnumber=1,
    tabsize=3,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    basicstyle=\color{Black}\ttfamily,
    commentstyle=\color{TMgreen},
    keywordstyle=\color{TMorange}\bfseries,
    stringstyle=\color{TMred},
    frame=leftline,
    framesep=0mm,
    xleftmargin=3mm,
    framesep=2mm,
    framerule=0mm,
    abovecaptionskip=5mm,
    aboveskip=\baselineskip,
    belowskip=\baselineskip
}

% need to use inner commands to avoid the verbatim nature
% of listing environments!

\lstnewenvironment{TMcode}[3]
{
    \lstset{style=TMstyle, language=#1, caption=#2}
    \mdfsetup{style=TMstyleCode}
    \mdframed
    \hspace*{3mm}
    \minipage{0.75cm}
    \includegraphics[width=\linewidth]{images/code2.png}
    \endminipage
    \hspace*{1mm}
    \minipage{\textwidth-1.05cm}
        {\sffamily\Large #3}
    \endminipage
    \vspace*{-2mm}
}
{
    \endmdframed
}

% We now want to create the bulletin-environments
% we first define two new mdenvironments, one for the header and one for
% the content
\newmdenv[
        skipabove=4mm,
        skipbelow=1mm,
        innertopmargin=1mm,
        innerbottommargin=1mm,
        innerleftmargin=0mm,
        innerrightmargin=0pt,
        rightline=false,
        topline=false,
        bottomline=false,
        linewidth=1mm,
        frametitlefont={\sffamily\bfseries},
        backgroundcolor=TMbulletinBackground]{TMbulletinBase}
\newmdenv[default, linewidth=0pt, backgroundcolor=TMbulletinBackground]{TMbulletinContent}

% we then define three styles, one for each type
\mdfdefinestyle{normal}{linecolor=TMnormal}
\mdfdefinestyle{warning}{linecolor=TMwarning}
\mdfdefinestyle{critical}{linecolor=TMcritical}

% we then define some auxilliary commands
\newcommand{\@TMbulletinTitleContent}[2]
{
    \hspace*{2mm}\begin{minipage}{0.75cm}
        \includegraphics[width=\linewidth]{#1}
    \end{minipage}\hspace*{1mm}\begin{minipage}{\textwidth-1.05cm}
            #2
    \end{minipage}
}

% and finally the main environment
% takes type (warning/normal/critical) as first argument
% takes bulletin title as second argument
\newenvironment{TMbulletin}[2]{
    \begin{TMbulletinBase}[style=#1, frametitle=\@TMbulletinTitleContent{images/#1.png}{#2}]
    \vspace*{1mm}
    \begin{TMbulletinContent}
}
{
    \end{TMbulletinContent}
    \end{TMbulletinBase}
}

% we now define the mdtable styles
\newcommand{\tableCaption}{}
\mdfdefinestyle{TMstyleTable}{
            skipabove=4mm,
            skipbelow=0mm,
            %remove borders
            rightline=false,
            topline=false,
            bottomline=false,
            linewidth=1mm,
            %margins
            innertopmargin=0mm,
            innerleftmargin=0mm,
            innerbottommargin=0mm,
            innerrightmargin=0pt,
            backgroundcolor=TMcodeBackground,
            linecolor=TMtableHead,
}
\everyrow{\tabucline[.4mm white]{}}
\tabulinesep=^3mm_2mm
\taburowcolors[2] 2{TMtableRowOne .. TMtableRowTwo}
\newenvironment{TMtable}[3]
{
    \renewcommand{\tableCaption}{#3}
    \begin{table}[#2]
    \begin{mdframed}[style=TMstyleTable]
    \begin{tabu} to \textwidth{#1}
        \rowfont{\bfseries\sffamily\leavevmode\color{white}}
        \rowcolor{TMtableHead!}
}
{
    \end{tabu}
    \end{mdframed}
    \caption{\tableCaption}
    \end{table}
}

% theorem styles
\declaretheoremstyle[
    postheadspace=\newline,
    headfont=\sffamily\bfseries\large,
    mdframed={
            skipabove=4mm,
            skipbelow=0mm,
            rightline=false,
            topline=false,
            bottomline=false,
            linewidth=1mm,
            linecolor=TMtheorem,
            backgroundcolor=TMcodeBackground,
    }
]{TMtheoremStyle}
\declaretheorem[style=TMtheoremStyle, within=section]{theorem}
\declaretheorem[style=TMtheoremStyle, sibling=theorem]{lemma}
\declaretheorem[style=TMtheoremStyle, sibling=theorem]{corollary}
\declaretheorem[style=TMtheoremStyle, sibling=theorem]{proposition}

答案1

更新:

根据您的编辑和评论,您似乎正在问的问题实际上问题是如何更改 pandoc 的 latex 编写器使用的命令。最透明、最直接的方法是编写一个 Lua 过滤器来更改代码块的生成方式。

第一步是弄清楚代码块在 pandoc 的 AST 中是如何内部表示的。你可以通过将示例转换为 来查看发生了什么nativepandoc --to native source.md其中source.md包含

```bash
    #!/bin/bash
    echo "hello world"
```

输出显示

[ CodeBlock
    ( "" , [ "bash" ] , [] )
    "    #!/bin/bash\n    echo \"hello world\""
]

现在,第二步是写一个lua 过滤器(我们称之为TMcode.lua)因为CodeBlock返回的是\begin{TMcode} . . . \end{TMcode}而不是\begin{lstlisting} . . . \end{lstlisting}

function CodeBlock(block)
  if FORMAT == "beamer" or FORMAT == "latex" then
    local raw_code = string.format(
      "\\begin{TMcode}%s\n%s\n\\end{TMcode}",
      block.classes[1] and "[language=" .. block.classes[1] .. "]" or "",
      block.text
    )
    return pandoc.RawBlock("latex", raw_code)
  end
end

最后,您可以致电pandoc --lua-filter=TMcode.lua --to latex source.md并获取

\begin{TMcode}[language=bash]
    #!/bin/bash
    echo "hello world"
\end{TMcode}

也许我不明白你的问题。 pandoc 中代码块的语法是:

```bash
    #!/bin/bash
    echo "hello world"
```

即代码块以刻度而不是点结束。

如果您想使用 lstlisting 环境,您必须使用选项调用 pandoc --listings

这将生成以下代码:

\begin{lstlisting}[language=bash]
    #!/bin/bash
    echo "hello world"
\end{lstlisting}

参见https://pandoc.org/MANUAL.html#extension-fenced_code_attributes

相关内容