定義 tex4ht 程式碼清單的替代環境,無需重複程式碼

定義 tex4ht 程式碼清單的替代環境,無需重複程式碼

我正在使用tcolorboxwithlistings進行程式碼格式化,它在 pdf 中運作良好,但該套件不適用於 tex4ht (它會產生無效的輸出)。

我很難弄清楚乳膠魔術程式碼需要告訴乳膠在使用 tex4ht 與 pdf 進行編譯時要做什麼,而不必複製程式碼片段本身。

一個小例子會有所幫助。目前這就是我所擁有的

\documentclass[12pt]{book}%
 ...
\begin{document}
\newtcblisting{....}{....code is here ...}  

問題是,這\newtcblisting是一個像包中的環境tcolorbox,上面的完整定義如下(MWE)

\documentclass[12pt]{book}%
\usepackage[T1]{fontenc}
\usepackage{color}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings}
\usepackage{matlab-prettifier}
\definecolor{bg}{RGB}{240,240,240}
\usepackage{upquote} %to fix string quotes
\usepackage{fancyvrb}    

\newtcblisting{matlab}[1]{
   nobeforeafter,colback=bg,size=minimal,hbox,listing only,
   listing options={style=Matlab-editor,basicstyle=\ttfamily#1,
      breaklines= false,escapechar= `,mlshowsectionrules = true
   }}

\begin{document}
\begin{matlab}{\small}
clear all;
m_zeros = [-1 -2];
m_poles = [0 -4 -6];
\end{matlab}
\end{document}

問題是,如何定義新環境,這將允許我在 tex4ht 中做一些不同的事情,而不必重複程式碼本身。即我當然可以這樣做:

 ...
 \ifdefined\HCode
  \begin{verbatim}%font size option not important, so no need to pass it
    ... code listing....
  \end{verbatim}
 \else
  \begin{matlab}{\small}%in pdf using tcblisting
      ... code listing....
  \end{matlab}
\fi
.....

但我不想重複程式碼本身,因為程式碼片段可能很大,而且文件本身就已經很複雜了。我也不想將這些程式碼片段放入檔案中,並出於多種原因從檔案中讀取它們。

這就是我被困的地方。我想這樣做:

....
\ifdefined\HCode 
   \newenvironment{matlab}[1]
   {\begin{verbatim}}
   {\end{verbatim}}
\else
\newtcblisting{matlab}[1]{
....

但上面的方法不起作用,因為那裡不允許逐字環境。所以我嘗試了這個

\DefineVerbatimEnvironment{matlabX}{Verbatim}{fontsize=\small}    
\ifdefined\HCode 
   \newenvironment{matlab}[1]
   {\begin{matlabX}}
   {\end{matlabX}}
\else
\newtcblisting{matlab}[1]{
 .....

這會導致 tex4ht 出現語法錯誤。在這一點上我已經超出了極限,並且達到了我在 Latex 代碼魔法方面的極限。不知該怎樣。

問題是:是否可以定義新的環境,例如現在逐字定義,在 tex4ht 運行時啟動,而不必複製程式碼本身?

使用命令將該檔案編譯為 HTML make4ht foo.tex,並foo.html在同一資料夾中產生。使用 編譯為 pdf lualatex foo.tex

答案1

listings對於 tex4ht 情況,為什麼不能使用新環境,因為無論如何都要載入它?

\documentclass[12pt]{book}%
\usepackage[T1]{fontenc}
\usepackage{color}
\usepackage{listings}
\usepackage{matlab-prettifier}
\definecolor{bg}{RGB}{240,240,240}
\usepackage{upquote} %to fix string quotes
\usepackage{fancyvrb}
\ifdefined\HCode
\lstnewenvironment{matlab}
{}
{}
\else
\usepackage{tcolorbox}
\tcbuselibrary{listings}
\newtcblisting{matlab}[1]{%
  nobeforeafter,
  colback=bg,
  size=minimal,
  hbox,
  listing only,
  listing options={%
    style=Matlab-editor,
    basicstyle=\ttfamily#1,
    breaklines= false,
    escapechar= `,
    mlshowsectionrules = true,
  },
}
\fi
\begin{document}
\begin{matlab}{\small}
clear all;
m_zeros = [-1 -2];
m_poles = [0 -4 -6];
\end{matlab}
\end{document}

答案2

這是使用包的方法moreverb

\documentclass[12pt]{book}%
\usepackage[T1]{fontenc}
\usepackage{color}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings}
\usepackage{matlab-prettifier}
\definecolor{bg}{RGB}{240,240,240}
\usepackage{upquote} %to fix string quotes

\newtcblisting{matlab}[1]{
   nobeforeafter,colback=bg,listing only,
   listing options={style=Matlab-editor,basicstyle=\ttfamily#1,
      breaklines= false,escapechar= `,mlshowsectionrules = true
   }}

\def\putVerb{%
  \HCode{<PRE>}%
  \verbatiminput{a.tex}%
  \HCode{</PRE>}%
}

\ifdefined\HCode
\usepackage{moreverb}
  \newenvironment{code}
  {\verbatimwrite{a.tex}}
  {\endverbatimwrite
  \aftergroup\putVerb}
\else
\usepackage{fancyvrb}
  \newenvironment{code}
  {\matlab{\small}}
  {\endmatlab}
\fi

\begin{document}
\begin{code}
clear all;
m_zeros = [-1 -2];
m_poles = [0 -4 -6];
\end{code}
and
\begin{code}
clear all;
m_poles = [0 -4 -6];
m_zeros = [-1 -2];
\end{code}

\end{document}

測試了htlatexpdflatex

相關內容