[英]LaTeX newcommand with verbatim or listings : problem with `#` (hash key)
我试图定义一个宏用\\newcommand
使用verbatim
或listings
环境。 由于verbatim
和listings
,似乎#1
中的散列键(代表参数)被转义。
我是宏的新手,所以我尝试了一些简单的东西:它适用于\\begin{center} ... \\end{center}
。
\documentclass[a4paper,oneside,11pt]{report}
\newcommand{\script}[1]{
\begin{center}
#1
\end{center}
}
\begin{document}
\script{blabla}
blibli
\end{document}
当我用verbatim
替换center
时,我收到此错误:
扫描使用@xverbatim时文件已结束。
或lstlisting
:
列表开始后文本被删除
我没有在stackoverflow上找到任何内容,也没有在https://tex.stackexchange.com上找到任何内容:您建议在宏中使用这些环境( \\newcommand
或者可能是\\newenvironment
)?
提前致谢
逐字内容很棘手。 你必须问自己意图是什么。 如果它是打印代码,那么山丘之王将是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}
找到一个使用\\verb
命令verbatim
解决的解决方法,并使用波浪号作为分隔符(如果我想在脚本中使用波浪号,我必须使用\\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
......
编辑:我刚刚注意到这个解决方法不会保持“自动”字符转义,所以它不是我想要的。 我希望能够在不转义特殊字符的情况下粘贴代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.