4

How can I make \emph{} look exaclty like this StackExchange highlighting.

I like the highlighting from StackExchange forum family and would like to use it in my Latex document.

Sorry I can't put my question into words, so I can't google it myself.

Johannes
  • 199

1 Answers1

9

You need to redefine the \emph command, and you will need the color package to provide you with a shaded box. Try this:

\documentclass{article}
\usepackage{color}
\definecolor{light-gray}{gray}{0.85}
\renewcommand\emph[1]{\colorbox{light-gray}{\texttt{#1}}}
\begin{document}

This is some \emph{marked} text.

\end{document}

enter image description here

However, there's a smarter way to do this that let's you use back ticks in the same way as SE does, using the newverbs and shortvrb packages:

\documentclass{article}
\usepackage{color}
\definecolor{light-gray}{gray}{0.85}
\usepackage{newverbs}
\usepackage{shortvrb}
\newverbcommand{\cverb}
{\setbox\verbbox\hbox\bgroup}
{\egroup\colorbox{light-gray}{\box\verbbox}}
\MakeSpecialShortVerb{\cverb}{\`}
%
\begin{document}

This is some `quoted` text, that allows `\TeX` macros to be shown without expansion.

\end{document}

enter image description here

But don't do this if you want to use the back tick for the left hand typographical quotation mark as Knuth originally intended in plain TeX.

And (as pointed out in the comments) putting things into a box like this prevents line breaks inside the box, so this approach is only suitable for short pieces of text, and you should be prepared to rewrite to avoid over- or under-full lines.

Thruston
  • 42,268
  • As far as I know, xcolor is preferred over color and is (at least largely) backwards compatible. – Sean Allred Sep 23 '14 at 17:21
  • 1
    xcolor would work just as well, but color is smaller, simpler, and not-yet-obsolete. Without more context or a MWE from the OP it's hard to tell which to prefer, so I went for simplicity. – Thruston Sep 23 '14 at 17:48
  • @Thruston This won't allow line breaks. – egreg Sep 23 '14 at 20:14
  • Thank You. The first solution with redefening \emph works fine for me. The second solution does not work. I suppose the problem is that I use XeLaTeX or maybe that I use \usepackage[babel, german=quotes]{csquotes} \MakeOuterQuote{"} – Johannes Sep 23 '14 at 21:03
  • To allow line breaks in highlighted text you can use the soul package. – Fritz Sep 23 '14 at 22:30
  • why is it so thick? – Ka Wa Yip Jul 05 '16 at 13:13
  • @kww It looks like it does, because that's how the Computer Modern Typewriter font was designed. You might prefer a different type writer font, or you could change the background colour - a lighter colour might make the font look less dense. – Thruston Jul 05 '16 at 19:53
  • @Thruston Thanks. Is there any way now to look exactly like stackexchange's. – Ka Wa Yip Jul 05 '16 at 23:15