我想为文本内嵌的代码或数学跨度提供“背景颜色”,就像 CSS 允许的那样。我曾尝试使用 来实现这一点\colorbox
,但不幸的是,这似乎会增加垂直对齐问题:
\documentclass{article}
\usepackage{blindtext}
\usepackage{xcolor}
\definecolor{light-gray}{gray}{0.89}
\begin{document}
\blindtext
$\mathsf{A \rightarrow P(x)}$
\blindtext
\colorbox{light-gray}{$\mathsf{A \rightarrow P(x)}$}
This is a sentence, after-which another formula with colour will be on the next line.
\colorbox{light-gray}{$\mathsf{A \rightarrow P(x)}$}
\blindtext
\end{document}
如果有意义的话,我更喜欢让颜色“渗入”专用于上方/下方线条的空间(即仍然是“高”,因为默认情况下它比行高高;但它不会影响所在行的有效行高。)
答案1
这是一个没有的版本\colorbox
(除了示例),但使用了非常好的\tcbhighmath
命令tcolorbox
和可配置的设置highlight math style={....}
最重要的设置是size=tight
,但还有size=minimal
!
\documentclass{article}
\usepackage{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{blindtext}
\definecolor{light-gray}{gray}{0.89}
\tcbset{highlight math style={enhanced jigsaw,size=tight,colback=light-gray,boxrule=0pt,sharp corners}}
\begin{document}
\blindtext
$\mathsf{A \rightarrow P(x)}$
\blindtext
\tcbhighmath{\mathsf{A \rightarrow P(x)}}
This is a sentence, after-which another formula with colour will be on the next line.
\colorbox{light-gray}{$\mathsf{A \rightarrow P(x)}$}
\blindtext
\end{document}
答案2
\colorbox
在四边添加填充;您想将其删除。
\documentclass{article}
\usepackage{blindtext}
\usepackage{xcolor}
\definecolor{light-gray}{gray}{0.89}
\newcommand{\graybg}[1]{%
\begingroup\setlength{\fboxsep}{0pt}% no padding
\colorbox{light-gray}{#1}%
\endgroup
}
\begin{document}
\blindtext
$\mathsf{A \rightarrow P(x)}$
\blindtext
\graybg{$\mathsf{A \rightarrow P(x)}$}
This is a sentence, after-which another formula with colour will be on the next line.
\graybg{$\mathsf{A \rightarrow P(x)}$}
\blindtext
\end{document}
如果您想要填充,但它不会干扰垂直间距,请打破颜色框,但添加具有实际尺寸的垂直幻影。
\documentclass{article}
\usepackage{blindtext}
\usepackage{xcolor}
\definecolor{light-gray}{gray}{0.89}
\newcommand{\graybg}[1]{%
\mbox{\vphantom{#1}\smash{\colorbox{light-gray}{#1}}}%
}
\begin{document}
\blindtext
$\mathsf{A \rightarrow P(x)}$
\blindtext
\graybg{$\mathsf{A \rightarrow P(x)}$}
This is a sentence, after-which another formula with colour will be on the next line.
\graybg{$\mathsf{A \rightarrow P(x)}$}
\blindtext
\end{document}
也许您想要两种方法的混合。默认值为\fboxsep
3pt。