# Why enclose a \verb block with curly braces?

Why would you enclose a \verb block with curly braces? Does it change anything? Consider the following examples:

The \verb|acmart| document is \verb|orange| and \verb|blue|.

Use the {\verb|verb|} command to insert in-line code.
You can also use {\verb|other commands like textt|}.

• In the code you posted, the extra pairs of curly braces do nothing except create code clutter. As to why anybody would add the curly braces, I have no sensible explanation. OCD, maybe? – Mico Jan 7 at 2:55
• I never used the curly braces to enclose only a \verb command and I'm still alive! Even sometimes, I do not use | , but something else , and still work ! So \relax ;-). What might not work is when the curly braces are the argument of a command (e.g.: \emph{\verb+plus+} is a "fatal error" (means no PDF), while {\em\verb+plus+} is syntactically correct, although stupid, because you cannot emphasize the verbatim text). – Fran Jan 7 at 6:09
• Vote to reopen. The question is perhaps naive but enough clear IMHO and I am willing to answer because could be useful for novices. – Fran Jan 7 at 11:41
• If this is a poll, then my answer is “there is no reason whatsoever for preferring {\verb|xyz|} to \verb|xyz|”. – egreg Jan 7 at 14:19
• All of your proposals in the answer suggests adding something in addition to the braces or how it is posted in the question. So, this question is still unclear to me. – Werner Jan 7 at 18:32

Why would you enclose a \verb block with curly braces?

To limit the scope of some other command also between the curly braces and before of \verb:

Lore {\color{red}\verb|ipsum|} dolor sit amet.


But note that the {...} group cannot be the argument of a command. The next code (supposedly equivalent) produces a fatal error:

Lore \textcolor{red}{\verb|ipsum|} dolor sit amet.


LaTeX Error: \verb illegal in command argument.

Does it change anything?

Nothing if the group contain only the \verb command. It can change a lot if there another commands in the group, as showed above, and even if there are some space before and/or after each brace.

One of the first things that LaTeX users learn is that extra spaces between words are ignored. But maybe take more time note that this is not true when there are curly braces among the spaces:

x                   x  % ---> produces "x x"

x{ { { { { } } } } }x  % --->  produces "x                   x"


And you must take into account that many macros without arguments can eat the next space-s, but obviously not not in they are after the left brace. Confusing at first, because many commands do not do that. This is a source for stupid mistakes even if you know well what you can expect in each case.

Therefore, if there are not a objective reason to embrace the verbatim text (as modify only it with another command) is safer and less prone to error is avoid them. In case of space eaters macros, a space after the left brace may seem a good idea, but is better empty group after the macro followed by one space (i.e.: {}) but left free the verbatim text. As matter of taste, you can replace {} by simply \ (or another spacer as ~, or \,, etc. if needed).

In summary, better an example that so much verbiage:

\documentclass[11pt]{article}
\begin{document}

Lore   \verb|ipsum| \par % OK (simpler, better?)
Lore  {\verb|ipsum|}\par % OK
Lore{  \verb|ipsum|}\par % OK
Lore { \verb|ipsum|}\par % Wrong! (space x2)

% but ...

\LaTeX     \verb|ipsum| \par % Wrong! (no space)
\LaTeX    {\verb|ipsum|}\par % Wrong! (no space)
\LaTeX{    \verb|ipsum|}\par % OK
\LaTeX   { \verb|ipsum|}\par % OK
\LaTeX{}   \verb|ipsum| \par % OK
\LaTeX\    \verb|ipsum| \par % OK (simpler, better?)

% but ...

Lore {\Large     \verb|ipsum|}\par  % OK (simpler, better?)
Lore {\Large    {\verb|ipsum|}}\par % OK
Lore {\Large{    \verb|ipsum|}}\par % Wrong  (space x2)
Lore {\Large {   \verb|ipsum|}}\par % Wrong  (space x2)

\end{document}

• It looks like you might enclose \verb in braces to make it easier to add additional modifiers at some later date, such as \color{red} For now, all we want to do is apply \verb to the word ipsum Maybe later we will apply several different modifiers to ipsum instead of just one. – IdleCustard Jan 8 at 15:17
• @IdleCustard Probably what you will intend could be done, better and easier, with the help of some package. To we aware of alternatives as minted, visit ctan.org/topic/verbatim and ctan.org/topic/listing for an overview. In case of specific languages as R you can do even "literate programing" (not listed in these topics) because is done with a R package (knitr) that can highlighted automatically the R code (and even run it). – Fran Jan 8 at 19:11