175

I would like to be able to make a single word in a text look like a coded word. Is there any command such as \code{...} which allows me to do so?

(basically, I want to produce something like above for the "\code{...}" part)

SRKX
  • 2,107

5 Answers5

211

Normally a monospaced font is used for this. This is accomplished with \texttt{...}. If you want to use code, you can use \def\code#1{\texttt{#1}}. From that point on you can write \code{...} to get monospaced output.

Roelof Spijker
  • 17,663
  • 5
  • 55
  • 63
  • Yes, that's the way to go. Put it semantically correct (by calling it code) and use texttt inside this until you come across a better formatting. – wal-o-mat Nov 24 '11 at 11:15
  • 39
    I'd prefer \newcommand{\code}[1]{\texttt{#1}} as you're dealing with LaTeX. Even better \newcommand{\code}{\texttt}, but this is quite advanced. – egreg Nov 24 '11 at 11:40
  • 6
    @egreg I'm not so sure about the last approach (yes, I know why you're doing it, but from an interface POV I prefer macros where the arguments required are clear). – Joseph Wright Nov 24 '11 at 11:41
  • 3
    @JosephWright That's why I wrote that it's advanced. :) – egreg Nov 24 '11 at 11:50
  • 7
    I would also wrap a \mbox around the \texttt, to avoid the word being hyphenated if it happens to fall near the end of a line. – Karl Ove Hufthammer Nov 24 '11 at 11:51
  • I would agree that \newcommand is slightly better. @KarlOveHufthammer: Regarding the \mbox I would disagree. It is perfectly possible to have long function names, possibly with arguments. You would surely want those to be wrapped. – Roelof Spijker Nov 24 '11 at 12:19
  • 2
    @wh1t3 Would you really want them hyphenated? That could become very confusing for the reader, especially for languages that use commands that can contain hyphens. For function names with arguments, I would define a separate command that handles this in a proper way, using penalties to get the optimal wrapping (e.g., never break the function name, but make is possible (but bad) to break at the comma between arguments). – Karl Ove Hufthammer Nov 24 '11 at 12:58
  • @KarlOveHufthammer: I don't want them hyphenated, but I don't want them mboxed either. Perhaps having an mbox around them to spot problems (over/underful hboxes) and then being able to deal with these issues is acceptable though. – Roelof Spijker Nov 24 '11 at 13:22
  • @wh1t3 You might also consider using the seqsplit package to allow breaking the ‘word’ everywhere, but without a hyphen. The result may be very ugly, though, with for example one letter before or after the break. – Karl Ove Hufthammer Nov 24 '11 at 15:06
  • 1
    @egreg: Reduce that to \let\code=\texttt altogether. – Andrey Vihrov Nov 24 '11 at 15:40
  • 1
    @KarlOveHufthammer: normally tt-fonts set \hyphenchar \font\m@ne and so disable hyphenation anyway. – Ulrike Fischer Nov 24 '11 at 15:54
  • 1
    \texttt{} what sort of command is that! How is anyone suppose to remember that? What does it stand for? Why so many t's tt? – Charlie Parker Oct 15 '19 at 16:12
  • 3
    @CharlieParker \texttt{} is named with the same logic as \textbf{}. That is, '\text' followed by the style abbreviated. The bf is short for bold face and tt is short for teletype which is the traditional name for monospaced fonts. – Mikko Rantalainen Nov 07 '19 at 07:27
  • @egreg 11 years later, but I can see you even have an answer here recommending something like \newcommand{\code}{\texttt}, but don't detail why you call it advanced. Are there gotchas to watch out for / any documentation that discusses the difference? – Kraigolas Aug 10 '22 at 17:18
  • 2
    @Kraigolas TeX does macro expansion. If you have \newcommand{\code}[1]{\texttt{#1}} when \code is expanded it reads its argument and passes it to \texttt which has to read it again. With \newcommand{\code}{\texttt} you don't read the argument twice. The overhead is negiligible, with modern machines. I'd not go with \let\code=\texttt for several reasons. – egreg Aug 10 '22 at 17:23
42

If you want a single word to look like a coded word and also to have a light-gray background as in StackExchange you can predefine a color \definecolor{light-gray}{gray}{0.95} and then define a new command: \newcommand{\code}[1]{\colorbox{light-gray}{\texttt{#1}}}.

From this point on you can use \code{word} to get mono-spaced words with gray background.

Of course for this to work you will need to load the xcolor package before \definecolor.

A full example would look like this:

% Better inline directory listings
\usepackage{xcolor}
\definecolor{light-gray}{gray}{0.95}
\newcommand{\code}[1]{\colorbox{light-gray}{\texttt{#1}}}
HaoZeke
  • 103
brescia123
  • 521
  • 4
  • 2
28

I can't believe nobody mentioned the listings package. It provides a command called \lstinline{your_code} which can even highlight keywords for you.

See also this question: Should I use \lstinline for the language keywords embedded in text?

Domi W
  • 380
  • 2
    Syntax: \lstinline[⟨key=value list⟩]⟨character⟩⟨source code⟩⟨same character⟩ Also, some engines support a wider working range of working delimiters. For exampe, I found ¿ to work with xelatex and not with pdflatex. – marc-medley Feb 18 '19 at 06:35
  • 1
    This should be the answer. No hands-on formatting where it can be avoided in LaTeX. – harmonica141 Sep 28 '20 at 12:45
  • This is the best answer and it could be mixed with others \newcommand{\code}[1]{\lstinline{#1}} – Víctor Herraiz Oct 29 '23 at 15:15
17

\verb|code| or \verb#code# also works. It creates characters in monospace, although its primary utility to enter commands that the compiler wont confuse as tex commands.

yayu
  • 5,237
  • 2
    Why does \verb use | or # to start and end code rather than the usual { and }? – user3728501 Nov 29 '16 at 14:52
  • 1
    @user3728501 what if I wanted to give an example LaTeX command using verbatim? I'd need different delimiters to indicate when to start/stop the verbatim text (since the { and } would need to be rendered as verbatim text). – Alex Nelson Aug 06 '18 at 13:44
2

I would recommend my package ffcode, which makes it as simple as this:

\documentclass{article}
\usepackage{ffcode}
\begin{document}
The function \ff{foo} can be used in a loop:
\begin{ffcode}
while (true) {
  foo(i++);
}
\end{ffcode}
\end{document}

The package uses minted for code blocks and tcolorbox for individual words.

yegor256
  • 12,021