How do I write, in LaTeX, something such as "Note that we have used \renewcommand several times .."? There is nothing special about \renewcommand here, it could be \usepackage, \textit, or any LaTeX command.
- 14,078
-
Potentially useful: https://tex.stackexchange.com/questions/2790/when-should-one-use-verb-and-when-texttt – LaTeXereXeTaL Jun 11 '21 at 02:59
1 Answers
Any time you want to specify some bit of text and not have it evaluated as a LaTeX command, you should wrap it in either \verb for in-line verbatim or, for multi-line text, use the verbatim environment. It also sets the text in the default typewriter font family for the document (usually Computer Modern Typewriter or its equivalent with alternative encoding). You want this since the default encoding of Computer Modern Roman (aka “The TeX font” does not include the expected characters for <, >, |, {, } or \ in those character positions.
For example, you would write:
Note that we have used \verb+\renewcommand+ several times
Note that \verb takes a different syntax for its argument than most LaTeX commands: It will take the next character following it and treat everything as verbatim until the next instance of the same character.¹
The verbatim environment is pretty straightforward and will preserve not only the characters but any line breaks³ as well. In both environments, spaces will be reproduced as entered.
Because of the way that LaTeX parses its arguments, you cannot put \verb or a verbatim environment in the argument to another command.⁴ The most full-proof way to set verbatim text in that context is to use \texttt and input any special characters using, e.g.
\char`\\
for any special characters that should be output where you prefix the special character after \char with a backtick and a backslash.⁵
There are a few exceptions to this. You can't, for example, use
{or}like this. If you use a letter, there needs to be a space between\verband the letter², spaces obviously won't work as the delimiting character, Unicode characters will work in XeLaTeX or LuaLaTeX but no pdfLaTeX, etc. Most folks stick with a handful of characters as their delimiters.For the more experienced LaTeX folk out there, had you ever considered the possibility of typing
\verb m\foom?You can't have a line break inside the argument to
\verbwhich makes sense since it's meant for in-line use where a line break is meaningless. LaTeX assumes that if it finds a line break inside the argument to\verb, it must be a mistake and will give you an error.This is not strictly true.
\indexwill let you put\verbin its argument. There's a package which allows\footnoteto take an argument which is parsed in a way that allows it to contain\verb(although I forget what it's called), although this is a fragile practice and prone to breaking unexpectedly. There is also a document class which modifies\footnotein the same way which, is anathema. Document classes should not change the behavior of standard commands in this manner.Perhaps there's a way, but I could not figure out how to do an in-line markdown text in verbatim of that particular string.
- 14,078