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)
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.
\newcommand{\code}[1]{\texttt{#1}}
as you're dealing with LaTeX. Even better \newcommand{\code}{\texttt}
, but this is quite advanced.
\mbox
around the \texttt
, to avoid the word being hyphenated if it happens to fall near the end of a line.
Commented
Nov 24, 2011 at 11:51
\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.
Commented
Nov 7, 2019 at 7:27
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}}}
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?
\lstinline[⟨key=value list⟩]⟨character⟩⟨source code⟩⟨same character⟩
Also, some engines support a wider working range of working <character> delimiters. For exampe, I found ¿
to work with xelatex and not with pdflatex.
Commented
Feb 18, 2019 at 6:35
\newcommand{\code}[1]{\lstinline{#1}}
Commented
Oct 29, 2023 at 15:15
\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.
\verb
use |
or #
to start and end code rather than the usual {
and }
?
Commented
Nov 29, 2016 at 14:52
{
and }
would need to be rendered as verbatim text).
Commented
Aug 6, 2018 at 13:44
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.
texttt{}
how that is an unhuman command. How is anyone suppose to remember this? If you figure out what the ttt's stand for please tell me!texttt
stands for text teletype. Similarly there are for example\textrm
, where rm stands for roman, and\textsf
where sf stands for serif.