10

What is the command to insert inline code words into the text like this? enter image description here

I know there are packages for inserting snippets into the code, but I need to insert keywords within the main text. I was wondering if there is a more convenient way to do that.

16

The package listings provides \lstinline for such short snippets. The advantage of such a package is its awareness of keywords etc.

In principle any formatting can be used for the 'code' -- as long as the syntax does not interfere with TeX/LaTeX syntax all is well.

The package xparse allows for verbatim arguments, it can be used as well.

\documentclass{article}

\usepackage{xcolor}
\usepackage{listings}

\usepackage{xparse}

\NewDocumentCommand{\codeword}{v}{%
\texttt{\textcolor{blue}{#1}}%
}

\lstset{language=C,keywordstyle={\bfseries \color{blue}}}

\begin{document}

\lstinline{for} or \lstinline{while} or \lstinline{main}

\codeword{Here} you \codeword{see} an \codeword{example} of \codeword{an} inline \codeword{code}

\end{document}

enter image description here

  • And of course you can specify your preferred font for code snippets in the \lstset, not just the colour/weight. – Chris H Jan 5 '16 at 13:58
13

This is also possible using the minted package and the highlighting tool pygmentize

The command for minted inline code is called \mintinline{language}{code}

The inline highlighting will only colorize keywords, that are defined for the language and colored properly according to the highlighting style you choose.

\documentclass{scrartcl}

\usepackage{minted}
\usemintedstyle{vs}

\begin{document}
    To create a table you could use the command \mintinline{MySQL}{ALTER TABLE}. 
    While  \mintinline{MySQL}{DROP TABLE} deletes a table,
    \mintinline{MySQL}{INSERT} enables you to insert something into a table.
\end{document}

Which gives you:

enter image description here

Note that the default highlighting style gives you green keywords and you need to use --shell-escape to get pygmentize to work.

  • 3
    +2 for using minted (-1 for using scrartcl :-P) – user31729 Jan 5 '16 at 16:02
  • Beware that installing pygmentize on some systems (windows) adds a level of complexity. – Fuhrmanator May 11 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.