如何创建一个行为类似于 \verb 的新 LaTeX 命令?

发布于 2024-08-12 03:50:31 字数 262 浏览 5 评论 0原文

我从现在起就一直在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

闻呓 2024-08-19 03:50:32

定义像逐字命令一样的新命令实际上是相当困难的。 LaTeX verbatim 包包含一些有用的东西。

如果您使用 times 包,Computer Modern 逐字字体与 PostScript times 字体不能很好地配合。如需更合适的逐字(打字机)字体,请尝试

\renewcommand{\ttdefault}{aett}

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

\renewcommand{\ttdefault}{aett}
时光清浅 2024-08-19 03:50:32

正如 Noah 所说, \verb 文本和正常输出的大小不应该不同。发布一个小型、完整的表单示例是一个好主意:

\documentclass{article}
\begin{document}
Text and \verb|verbatim % text|.
\end{document}

如果您想选取文本来逐字处理格式,那么由于需要观看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:

\documentclass{article}
\begin{document}
Text and \verb|verbatim % text|.
\end{document}

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.

喜爱纠缠 2024-08-19 03:50:32

将以下行放入文档包含的 .sty 文件中:
\renewcommand{\verbatim@font}{\ttfamily\small}

Put the following line in a .sty file included by your document:
\renewcommand{\verbatim@font}{\ttfamily\small}

海风掠过北极光 2024-08-19 03:50:32

让我举一些新命令的例子:

\newcommand{\zope}{Zope}

现在,无论我在 Latex 文本中给出 \zope ,它都会被上面指定的文本替换。如果我需要进行全球性的改变,我可以做到。

\newcommand{\bi}{\begin{itemize}}
\newcommand{\ei}{\end{itemize}}

在上面两个命令的帮助下,现在我可以将列表编写为:

\bi
\item 1st item
\item 2nd item 
\item and so on...
\ei

这说明了如何在 newcommand 的帮助下缩短某些命令。

现在让我们考虑一个带有参数的新命令。

\newcommand{\filepath}[1]{\verb!#1!}

要使用此命令,我会编写类似 \filepath{abc.xml} 的内容,在处理命令时将其替换为 \verb!abc.xml!

另一个例子:

\newcommand{\ida}[1]{\textcolor{blue}{#1}}

借助 \ida{myVariable} 替换为 \textcolor{blue}{myVariable}

我希望这个小教程将有助于达到目的。

Let me give some examples of new command:

\newcommand{\zope}{Zope}

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.

\newcommand{\bi}{\begin{itemize}}
\newcommand{\ei}{\end{itemize}}

With the help of the two commands above, now I can write my lists as:

\bi
\item 1st item
\item 2nd item 
\item and so on...
\ei

This illustrates how some commands can be shortened with the help of newcommand.

Now lets consider a newcommand with a parameter.

\newcommand{\filepath}[1]{\verb!#1!}

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:

\newcommand{\ida}[1]{\textcolor{blue}{#1}}

With help of this \ida{myVariable} is replaced with \textcolor{blue}{myVariable}

I hope this small tutorial will help serve the purpose.

二货你真萌 2024-08-19 03:50:31

为什么不使用 \texttt{} 命令来获取等宽文本?

我不确定为什么 \verb 命令为您提供比正常文本更大的文本(对我来说,\verb 与常规文本的大小完全相同,并且 \texttt{} 也是如此)。但是,如果您确实想创建一个新命令来获得更小的等宽文本,您可以尝试在标头中定义一个命令,如下所示:

\newcommand{\smalltt}[1]{{\small\texttt{#1}}}

其中 [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:

\newcommand{\smalltt}[1]{{\small\texttt{#1}}}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文