
由於在我的文件中,我必須在彩色框中寫入大量數學內容,因此我想避免在每個框中寫入 $ 。有沒有辦法newtcbox
在數學模式下定義?
我嘗試過\newtcbox{\mywbox}[1]{<options>}{$\displaystyle #1$}
但它不起作用。
這是我的乳膠代碼:
\usepackage{tcolorbox}
\newtcbox{\mywbox}{on line,colback=white,colframe=black,size=fbox,arc=3pt,boxrule=0.8pt}
\begin{document}
\obeylines
This \mywbox{$3x$} is in line math.
This $$y=\mywbox{$-5x$}-5+6$$ is not in line math.
\end{document}
我在官方 tcolorbox 手冊中閱讀了有關\tcboxmath
和的內容\tcbhighmath
,但我不明白如何將它們簡單地定義為 newtcbox 以及它們是否是我所需要的。
答案1
我會說
\newtcbox{\mywboxtext}{on line,colback=white,colframe=black,size=fbox,arc=3pt,boxrule=0.8pt}
\newcommand{\mywboxmath}[1]{\mywboxtext{$#1$}}
你的例子可以變成
\documentclass{article}
\usepackage{tcolorbox}
\newtcbox{\mywboxtext}{on line,colback=white,colframe=black,size=fbox,arc=3pt,boxrule=0.8pt}
\newcommand{\mywboxmath}[1]{\mywboxtext{$#1$}}
\begin{document}
This \mywboxmath{3x} is in line math and this
\[
y=\mywboxmath{-5x}-5+6
\]
is display math.
\end{document}
根據您的喜好更改名稱。
避免$$
在 LaTeX 中(當然還有\obeylines
)。
如果您需要框在下標或上標中表現,請將程式碼變更為
\documentclass{article}
\usepackage{tcolorbox}
\newtcbox{\mywboxtext}{on line,colback=white,colframe=black,size=fbox,arc=3pt,boxrule=0.8pt}
\makeatletter
\newcommand{\mywboxmath}[1]{\mathpalette\mywboxmath@do{#1}}
\newcommand{\mywboxmath@do}[2]{\mywboxtext{$\m@th#1#2$}}
\makeatother
\begin{document}
This $\mywboxmath{3x}$ is in line math and this
\[
y_{\mywboxmath{0}}=\mywboxmath{-5x}-5+6
\]
is display math.
\end{document}
請注意,在這種情況下,您需要\mywboxmath
處於數學模式。
答案2
您可以使用以下程式庫\tcboxmath
來執行此操作:theorems
tcolorbox
\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}
\newcommand*{\mywbox}{%
\tcboxmath[colback=white, colframe=black, size=fbox, arc=3pt, boxrule=0.8pt]%
}
\begin{document}
This \mywbox{3x} is in line math.
This
\[ y = \mywbox{-5x} - 5 + 6 \]
is not in line math.
\end{document}
您可能還想使用樣式對事物進行一些抽象,以便在需要時可以將其套用到其他框框:
\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}
\tcbset{my math box/.style={
colback=white, colframe=black, size=fbox, arc=3pt, boxrule=0.8pt}
}
\newcommand*{\mywbox}{\tcboxmath[my math box]}
\begin{document}
This \mywbox{3x} is in line math.
This
\[ y = \mywbox{-5x} - 5 + 6 \]
is not in line math.
\end{document}
讓盒子的背景透明
這可以透過使用/tcb/opacityback
皮膚來完成拼圖框架引擎,例如standard jigsaw
或enhanced jigsaw
。
\documentclass[fleqn]{article}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}
\tcbset{
my math box/.style={
standard jigsaw, % useful for 'opacityback' and 'opacityframe'
colback=green!20, colframe=black, size=fbox, arc=3pt, boxrule=0.8pt,
opacityback=0.6,
}
}
\newcommand*{\mywbox}{\tcboxmath[my math box]}
\begin{document}
This \makebox[0pt]{%
\raisebox{-0.5\height}[0pt][0pt]{\hspace{1cm}\includegraphics{example-image-duck}}%
}%
\mywbox{3x} is in line math.
This
\[ y = \mywbox{-5x} - 5 + 6 \]
is not in line math.
\end{document}