macros - 带有逐字或列表的 LaTeX 新命令:`#` 问题(哈希键)
问题描述
我正在尝试\newcommand
使用使用verbatim
或listings
环境定义宏。#1
由于verbatim
and ,(代表参数)中的哈希键似乎被转义了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
)中使用这些环境吗?
提前致谢
解决方案
逐字逐句的内容很棘手。您必须问自己的意图是什么。如果是打印代码,那么山中之王就是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}
推荐阅读
- reactjs - 使用后退按钮时 React JS 页面不断刷新
- python - 如何使用 seaborn 制作气泡图
- c++ - OpenGL - 纹理未正确渲染
- php - 使用带有 key=>value 的 foreach 循环进行迭代,但为每行输出分配不同的变量
- python-3.x - 分类问题 - 每条记录都是唯一的类
- javascript - Javascript 登录系统检查是否 ("UserPassword") == Undefined 不起作用
- go - 如何从 RGB 颜色中获取十六进制颜色?
- java - 使用 pdfbox java 为签名文件添加自定义值
- laravel - “npm run dev”时 ./resources/js/components/ExampleComponent.vue 1:0 中的错误
- java - 我无法理解在 java 的泛型中使用传递接口