如何创建一个行为类似于 \verb 的新 LaTeX 命令?
我从现在起就一直在使用 LaTeX,但从来没有真正亲自声明过一个新命令,因为我试图避免这种情况。
但是,我需要经常在文档中添加等宽文本,并且我使用 \verb
来实现它,这很好,只是动词字体大小比普通文本字体大。因此,我需要更改字体大小并撤消完成,例如 \small{}\verb#My monospace code#\normalsize{}
。这样不太方便,而且容易出错。
有更好的方法吗?我可以为此定义一个新命令吗?如何?
I have been using LaTeX from sometime now, but have never actually gotten my hands dirty declaring a new command, as I try to avoid that.
However, I need to add monospace text often in my document and I use \verb
for it which is fine, except that the verb font size is bigger than the normal text font. So I need to change the font size and undo done like \small{}\verb#My monospace code#\normalsize{}
. This is not very convenient and mistake-prone.
Is there a better way to do this? Can I define a new command for this? How?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
定义像逐字命令一样的新命令实际上是相当困难的。 LaTeX
verbatim
包包含一些有用的东西。如果您使用
times
包,Computer Modern 逐字字体与 PostScript times 字体不能很好地配合。如需更合适的逐字(打字机)字体,请尝试Defining new commands that act like the verbatim commands is actually quite difficult. The LaTeX
verbatim
package contains some stuff that will help.If you are using the
times
package, the Computer Modern verbatim font does not go well with the PostScript times fonts. For a more suitable verbatim (typewriter) font try正如 Noah 所说,
\verb
文本和正常输出的大小不应该不同。发布一个小型、完整的表单示例是一个好主意:如果您想选取文本来逐字处理格式,那么由于需要观看catcodes,事情会有点复杂。我会为此查看 fancyvrb 或列表包。
As Noah says, the size should not be different between
\verb
text and normal output. It would be a good idea to post a small, complete example of the form:If you want to pick up text to process verbatim with formatting, things are a bit complicated by the need to watch catcodes. I'd look at the fancyvrb or listings packages for this.
将以下行放入文档包含的 .sty 文件中:
\renewcommand{\verbatim@font}{\ttfamily\small}
Put the following line in a .sty file included by your document:
\renewcommand{\verbatim@font}{\ttfamily\small}
让我举一些新命令的例子:
现在,无论我在 Latex 文本中给出
\zope
,它都会被上面指定的文本替换。如果我需要进行全球性的改变,我可以做到。在上面两个命令的帮助下,现在我可以将列表编写为:
这说明了如何在 newcommand 的帮助下缩短某些命令。
现在让我们考虑一个带有参数的新命令。
要使用此命令,我会编写类似
\filepath{abc.xml}
的内容,在处理命令时将其替换为\verb!abc.xml!
。另一个例子:
借助
\ida{myVariable}
替换为\textcolor{blue}{myVariable}
我希望这个小教程将有助于达到目的。
Let me give some examples of new command:
Now whereever I give
\zope
in my Latex text, its replaced by the text specified above. This if I need to make a global change I can make it.With the help of the two commands above, now I can write my lists as:
This illustrates how some commands can be shortened with the help of newcommand.
Now lets consider a newcommand with a parameter.
To use this command, I would write something like
\filepath{abc.xml}
which will be replaced by\verb!abc.xml!
on processing the command.Another example:
With help of this
\ida{myVariable}
is replaced with\textcolor{blue}{myVariable}
I hope this small tutorial will help serve the purpose.
为什么不使用
\texttt{}
命令来获取等宽文本?我不确定为什么
\verb
命令为您提供比正常文本更大的文本(对我来说,\verb
与常规文本的大小完全相同,并且\texttt{}
也是如此)。但是,如果您确实想创建一个新命令来获得更小的等宽文本,您可以尝试在标头中定义一个命令,如下所示:其中
[1]
指示该命令采用一个参数 (文本变小且等宽。)那么当然您可以将其用作\smalltt{我较小的等宽文本。}
。Why not use the
\texttt{}
command to get monospaced text?I'm not sure why the
\verb
command is giving you text that's larger than normal (for me,\verb
is exactly the same size as regular text, and\texttt{}
as well). However, if you really want to make a new command to get you smaller monospaced text, you might try defining a command in your header as follows:Where the
[1]
indicates that the command takes one argument (the text to make small and monospaced.) Then of course you'd use it as\smalltt{my smaller monospaced text.}
.