1

I'm trying to recreate markdown-style inline code blocks in latex, you know, these things. They should have a slightly highlighted background, but not influence line height or word separation of the text they are defined in. Also, blocks with tall content like | should have the same height as others like -.

This is my attempt recreating them in latex:

\documentclass[a4paper, 12pt, hidelinks]{report}

\usepackage{tcolorbox} \newcommand{\code}[1]{\mbox{ \ttfamily \tcbox[ on line, boxsep=0pt, left=4pt, right=4pt, top=2pt, bottom=1.5pt, toprule=0pt, rightrule=0pt, bottomrule=0pt, leftrule=0pt, oversize=0pt, enlarge left by=0pt, enlarge right by=0pt, colframe=white, colback=black!12 ]{#1} }}

\begin{document}

This is a \code{test} for markdown-style \code{code blocks}.

Plus \code{+} Minus \code{-}

\end{document}

Unfortunately, this attempt creates extensive word spacing, unwanted space before punctuation, and doesn't have uniform height:

Screenshot

Can someone tell me how to improve these code blocks?

EDIT: The unwanted space can be removed by adding % to the end of some lines in the macro. Thanks to @Teepeemm for suggesting this fix in the comments.

msrd0
  • 117
  • 1
    For the spacing before and after, add % to the end of some of your lines. See https://tex.stackexchange.com/q/7453/107497 – Teepeemm Oct 17 '22 at 13:45

2 Answers2

2

With Tikz: baseline, anchor=base and minimum height

enter image description here

    \documentclass[a4paper, 12pt]{report}
    %https://tex.stackexchange.com/questions/661993/recreate-markdown-style-inline-code-blocks-in-latex
    \usepackage{tikz}
    \tikzset{%
        baseline,
        inner sep=2pt,
        minimum height=12pt,
        rounded corners=2pt  
    }
    \newcommand{\code}[1]{\mbox{% added this percent
        \ttfamily
        \tikz \node[anchor=base,fill=black!12]{#1};% added this percent
    }}
    \begin{document}
    \tikzset{%
        baseline,
        inner sep=2pt,
        minimum height=12pt
    }
    This is a \code{test} for markdown-style \code{code blocks} .
Plus \code{+} Minus \code{-}.
\end{document}

pascal974
  • 4,652
0

The left and right spacing is caused by missing % at the end of lines in your command definition. See What is the use of percent signs (%) at the end of lines? (Why is my macro creating extra space?)

For the height, I just specified a height to the tcbox; .8 seemed about right.

\documentclass[a4paper, 12pt, hidelinks]{report}

\usepackage{tcolorbox} \newcommand{\code}[1]{\mbox{% added this percent \ttfamily \tcbox[ on line, boxsep=0pt, left=4pt, right=4pt, top=2pt, bottom=1.5pt, toprule=0pt, rightrule=0pt, bottomrule=0pt, leftrule=0pt, oversize=0pt, enlarge left by=0pt, enlarge right by=0pt, colframe=white, colback=black!12, height=.8\baselineskip % added this (and guessed at .8) ]{#1}% added this percent }}

\begin{document}

This is a \code{test} for markdown-style \code{code blocks}.

Plus \code{+} Minus \code{-}.

\end{document}

code output

Teepeemm
  • 6,708
  • Thanks for your answer, the % works great. However, using height=.8\baselineskip doesn't work as this offsets the baseline for all characters. It looks like all characters are now starting .8\baselineskip above baseline, and go as far down as they want. Instead, I'd like to not change character position and only set the height of the background if possible. – msrd0 Oct 17 '22 at 14:06