带逐字或列表的LaTeX newcommand:`#`(哈希键)问题

时间:2019-01-18 09:40:04

标签: macros latex special-characters listings verbatim

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

我是宏的新手,所以我尝试了一些简单的方法:它可以与\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)中使用那些环境有什么建议?

预先感谢

2 个答案:

答案 0 :(得分:3)

逐字记录内容比较棘手。您必须问自己意图是什么。如果正在打印代码,那么山丘之王就是listings。我建议这样做,并为大量特定于代码的输出定义自己的环境。

这是一个例子:

enter image description here

\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}

答案 1 :(得分:0)

使用SecondLineManager命令为<link rel="stylesheet/less" type="text/css" href="styles.less" /> 找到了一种解决方法,并使用波浪号作为定界符(如果我想在脚本中使用波浪号,则必须使用verbatim):

\verb

但是\textasciitilde没什么...


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