7

I have to include small code snippets like method and class names within continuous text. I do not want to display those snippets as complete seperated listings, they should be displayed inline, similar to the option this site offers with backticks.

But let's face it, the options that are available out of the box are just plain ugly.

What I have in mind is the notation GitHub (and many other sites) use:

So, in particular:

  • rounded corners
  • contrastring border color
  • different background color
  • fixed width code font
  • pretty (vertical) alignment within the text

I found many options to draw boxes around text (fancybox, colorbox, mdframed and others) but none of those packages seemed to be capable to fulfill all of my requirements (then again, I'm not really a LaTeX expert).

So the question is: How can we make inline code pretty?

1 Answer 1

8

Is this what you're looking for?

Output

enter image description here

Code

\documentclass{article}
\usepackage{tikz}

\newcommand\code[1]{
  \tikz[baseline=(s.base)]{
    \node(s)[rounded corners,fill=orange!20,draw=gray]{\texttt{#1}};
  }
}

\begin{document}

Further, lists can be turned into \textcolor{blue}{Task Lists} 
by prefacing list items with \code{[ ]} or \code{[x]} (incomplete or complete,
respectively.

\end{document}

Update

A slight improvement of the \code macro, to ensure uniform height and depth of the box regardless of the height/depth of its content. Also, the improved macro \codex admits an optional argument.

\newcommand\codex[2][]{
    \tikz[baseline=(s.base)]{
        \node(s)[
            rounded corners,
            fill=blue!5,        % background color
            draw=gray,          % border of box
            text=gray!50!black, % text color
            inner xsep =3pt,    % horizontal space between text and border
            inner ysep =0pt,    % vertical space between text and border
            text height=2ex,    % height of box
            text depth =1ex,    % depth of box
            #1                  % other options
        ]{\texttt{#2}};
    }
}

Compare the output of \code and the improved version \codex

\code{aaaa} \code{bbbb} \code{ffff} \code{gggg} \code{<code>\textbackslash code</code>}

\codex{aaaa} \codex{bbbb} \codex{ffff} \codex{gggg} \codex{<code>\textbackslash code</code>}

enter image description here

10
  • Looks very promising :) , what is missing is a (thin) border around the box
    – Leo
    Oct 22, 2013 at 22:27
  • @Mef: see updated answer. The box can be drawn by adding draw=<color> option to the node operation.
    – Herr K.
    Oct 22, 2013 at 22:30
  • Nice work! One more thing to make it perfect: Can the text color also be changed within this command (dark grey instead of black)?
    – Leo
    Oct 22, 2013 at 22:32
  • @Mef Add text=gray!50!black into the option of the \node. Oct 22, 2013 at 23:07
  • 1
    @Mef: I've made a slight improvement of the macro to ensure uniform height and depth of the box despite its content. See update in the answer.
    – Herr K.
    Oct 23, 2013 at 16:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .