簡體   English   中英

Newcommand掛斷LaTeX編譯

[英]Newcommand hangs LaTeX compilation

我在LaTeX中為lstlisting定義新命令時遇到問題。 結果是編譯掛起而沒有錯誤消息,只是一個*

這掛起了LaTeX編譯:

\documentclass[10pt,a4paper,ssfamily]{exam}
\usepackage{listings}
\newcommand{\cs}{\begin{lstlisting}}
\newcommand{\ce}{\end{lstlisting}}
\begin{document}
\cs
program test
  ! This is a commentary
end program test
\ce
\end{document}

但是這個沒有:

\documentclass[10pt,a4paper,ssfamily]{exam}
\usepackage{listings}
\begin{document}
\begin{lstlisting}
program test
  ! This is a commentary
end program test
\end{lstlisting}
\end{document}

唯一的區別在於,開始和endmargin的和的端lstlisting在一個新的命令,在第一個地方所定義。 例如,對於lstlisting會發生此問題,而對於\\begin{center}不會發生此問題。

好吧,我發現了一個類似的問題,在這里報告:

https://tex.stackexchange.com/questions/25597/wrapping-code-listings-verbatim-or-other-method-inside-a-newcommand

似乎不可能做我想要的事情,因為\\ begin {lstlisting}的編譯需要搜索相應的\\ end {lstlisting}並且new命令會破壞它。

但是,定義新的lst環境可以獲得相同的結果,例如:

 \\lstnewenvironment{code}{\\lstset{language=[90]Fortran, xleftmargin=1.5cm }}{} 

例如。 然后,可以定義代碼段的許多屬性,並且語法更容易(\\ begin {code},\\ end {code})。

在我的例子中,完整的新環境是:

 \\lstnewenvironment{code}{\\lstset{language=[90]Fortran, basicstyle=\\ttfamily, keywordstyle=\\color{blue}, commentstyle=\\color{gray}, xleftmargin=1.5cm, morecomment=[l]{!\\ }% Comment only with space after ! }}{} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM