我正在尝试在 latex 中重新创建 markdown 样式的内联代码块,你知道的these things
。它们应该有一个略微突出显示的背景,但不会影响它们所定义文本的行高或单词分隔。此外,具有高内容的块|
应该与其他块具有相同的高度,例如-
。
这是我在乳胶中重新创建它们的尝试:
\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}
不幸的是,这种尝试会造成大量的单词间距、标点符号前不必要的空格,并且高度也不统一:
有人能告诉我如何改进这些代码块吗?
编辑%
:可以通过在宏中的某些行末尾添加空格来删除不需要的空格。感谢@Teepeemm 在评论中建议此修复。
答案1
包括Tikz
:baseline
,anchor=base
和minimum height
\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}
答案2
左右间距是由于%
命令定义中缺少行尾导致的。请参阅行尾的百分号 (%) 有什么用?(为什么我的宏会产生额外的空间?)
对于高度,我只是为 tcbox 指定了一个高度;.8 似乎差不多。
\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}