Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

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

share|improve this answer
    
Looks very promising :) , what is missing is a (thin) border around the box –  Mef Oct 22 '13 at 22:27
    
@Mef: see updated answer. The box can be drawn by adding draw=<color> option to the node operation. –  Kevin C Oct 22 '13 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)? –  Mef Oct 22 '13 at 22:32
    
@Mef Add text=gray!50!black into the option of the \node. –  Paul Gaborit Oct 22 '13 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. –  Kevin C Oct 23 '13 at 16:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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