1

我正在尝试\newcommand使用使用verbatimlistings环境定义宏。#1由于verbatimand ,(代表参数)中的哈希键似乎被转义了listings

我是宏的新手,所以我尝试了一些简单的方法:它适用于 \begin{center} ... \end{center}.

\documentclass[a4paper,oneside,11pt]{report}
\newcommand{\script}[1]{
  \begin{center}
    #1
  \end{center}
}
\begin{document}
  \script{blabla}
  blibli
\end{document}

当我替换center为 时verbatim,我收到此错误:

扫描使用 @xverbatim 时文件结束。

lstlisting

列表开始后删除的文本

我在 stackoverflow 和https://tex.stackexchange.com上都没有找到任何东西:你会建议在宏(\newcommand或者也许\newenvironment)中使用这些环境吗?

提前致谢

4

2 回答 2

3

逐字逐句的内容很棘手。您必须问自己的意图是什么。如果是打印代码,那么山中之王就是listings. 我建议并为大量特定于代码的输出定义自己的环境。

这是一个例子:

在此处输入图像描述

\documentclass{article}

\usepackage{listings}

\lstnewenvironment{code}[1][]
  {\lstset{#1}}% Add/update settings locally
  {}

\lstset{% Global options
  frame = single,
  basicstyle = \ttfamily\small,
  language = PHP
}

\begin{document}

My first PHP ``Hello World'' page:

\begin{code}
<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'; ?> 
 </body>
</html>
\end{code}

When you write \lstinline!<title>PHP Test</test>!, it sets the \textit{title} of the page.

\end{document}
于 2019-01-18T17:54:26.270 回答
0

找到了verbatimwith\verb命令的解决方法,并使用波浪号作为分隔符(如果我想在脚本中使用波浪号,我必须使用\textasciitilde):

\documentclass{article}

\newcommand{\scr}[1]{
    \begin{minipage}{0.9\textwidth} 
        \fbox{
            \parbox{\textwidth}{            
                \verb~#1~               % <-- HERE
            }
        }
    \end{minipage}  
}   

\begin{document}        
    \scr{Some script code here... 

    here a tilde : \textasciitilde
    }
\end{document}

但没有什么listings...


编辑:我刚刚注意到这种解决方法不会让“自动”字符转义,所以这不是我想要的。我希望能够在不转义特殊字符的情况下粘贴代码。

于 2019-01-18T13:38:37.300 回答