我喜欢\boxed
在家庭作业问题集中使用来强调我的答案。但是,每当我在框内使用上标/指数时,框就会变得比看起来应该的高度高。这会导致两个问题:框看起来很糟糕,并且会推挤其他文本行。
这是一个例子。请注意,直到我在框内使用指数时,行距才会改变。
有\boxed
没有其他方法可以避免这个问题?或者有其他好的方法来解决这个问题?
答案1
您可以tikzmark
使用Andrew Stacey 的回答标记您想要放置框的端点。\MyBox
下面定义的宏接受可选的第一个参数,它允许您获取精美的框。
笔记:
- 这确实需要两次运行:第一次计算框的位置,第二次将其绘制在正确的位置。
已知的问题:
如果文本跨越行边界,则此方法无效。如果这是个问题,并且您愿意考虑使用突出显示而不是框,则应参考LaTeX 中的酷炫文本突出显示。
此版本无法正确调整垂直间距的文本(例如
\dfrac{1}{2}
来自amsmath
包装的文本)。但是,可在此处找到可调整到正确高度的更新版本具有最小间距的 \boxed 替代品?
代码:
\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,shapes}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawBox}[1][red]{%
\tikz[overlay,remember picture]{
\draw[#1]
($(bl)+(-0.2em,0.9em)$) rectangle
($(br)+(0.2em,-0.3em)$);}
}
\newcommand{\MyBox}[2][red]{\tikzmark{bl}#2\tikzmark{br}\DrawBox[#1]}
\begin{document}
Lorem ipsum dolor sit amet. \MyBox{Lorem ipsum} dolor sit amet. Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem
ipsum \MyBox[blue]{$3.29 \times 10^{29}$} dolor sit amet. Lorem ipsum dolor sit amet. Lorem
ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem
\MyBox[draw=red,fill=yellow!20,,opacity=0.3]{$3.29 \times 10^{29}$} ipsum dolor sit amet.
Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.
\end{document}
答案2
如果您有兴趣在外观方面复制 的行为\boxed
,但删除与高度相关的添加(由\fboxrule
和提供\fboxsep
),您可以根据 定义自己的装箱宏\boxed
。这是\boxed
在 中定义的amsmath
:
\newcommand{\boxed}[1]{\fbox{\m@th$\displaystyle#1$}}
因此,定义\myboxed
为
\newcommand{\myboxed}[1]{%
\rlap{\hspace*{\dimexpr\fboxrule+\fboxsep\relax}%
\phantom{\m@th$\displaystyle#1$}}%
\smash{\boxed{#1}}}
即可实现此目的。以下是 MWE,显示了两个段落的结果输出;第一个段落使用\boxed
,而第二个段落使用\myboxed
:
\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\makeatletter
\newcommand{\myboxed}[1]{%
\rlap{\hspace*{\dimexpr\fboxrule+\fboxsep\relax}%
\phantom{\m@th$\displaystyle#1$}}%
\smash{\boxed{#1}}}
\makeatother
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Aliquam ligula dui, blandit et posuere eu, varius in erat.
Nunc ornare tellus in dui \boxed{3.299\times 10^{29}} tempus facilisis. Nunc ornare tellus
in dui tempus facilisis. \boxed{3.299} Nulla vel dolor augue. Nulla commodo nulla et tortor
aliquam nec lacinia odio sagittis. Aenean vitae interdum dui. Lorem ipsum dolor sit amet,
consectetur adipiscing elit. $3.299\times 10^{29}$ Aliquam ligula dui, blandit et posuere
eu, varius in erat. Nunc ornare tellus in dui tempus facilisis.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Aliquam ligula dui, blandit et posuere eu, varius in erat.
Nunc ornare tellus in dui \myboxed{3.299\times 10^{29}} tempus facilisis. Nunc ornare
tellus in dui tempus facilisis. \myboxed{3.299} Nulla vel dolor augue. Nulla commodo nulla
et tortor aliquam nec lacinia odio sagittis. Aenean vitae interdum dui. Lorem ipsum dolor
sit amet, consectetur adipiscing elit. $3.299\times 10^{29}$ Aliquam ligula dui, blandit et
posuere eu, varius in erat. Nunc ornare tellus in dui tempus facilisis.
\end{document}
您会注意到,它还突出显示了删除垂直框调整的一些问题。@Boris 的建议\fboxsep
会有所帮助。您还可以打印一些更微妙的东西,比如阴影框,然后删除实线。以下是实现此目的的替代方法\myboxed
:
\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\makeatletter
\newcommand{\myboxed}[2][black!5]{%
\setlength{\fboxrule}{0pt} \setlength{\fboxsep}{1pt}%
\rlap{\hspace*{\fboxsep}%
\phantom{\m@th$\displaystyle#2$}}%
\smash{\colorbox{#1}{\m@th$\displaystyle#2$}}}
\makeatother
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Aliquam ligula dui, blandit et posuere eu, varius in erat.
Nunc ornare tellus in dui \boxed{3.299\times 10^{29}} tempus facilisis. Nunc ornare tellus
in dui tempus facilisis. \boxed{3.299} Nulla vel dolor augue. Nulla commodo nulla et tortor
aliquam nec lacinia odio sagittis. Aenean vitae interdum dui. Lorem ipsum dolor sit amet,
consectetur adipiscing elit. $3.299\times 10^{29}$ Aliquam ligula dui, blandit et posuere
eu, varius in erat. Nunc ornare tellus in dui tempus facilisis.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Aliquam ligula dui, blandit et posuere eu, varius in erat.
Nunc ornare tellus in dui \myboxed{3.299\times 10^{29}} tempus facilisis. Nunc ornare
tellus in dui tempus facilisis. \myboxed{3.299} Nulla vel dolor augue. Nulla commodo nulla
et tortor aliquam nec lacinia odio sagittis. Aenean vitae interdum dui. Lorem ipsum dolor
sit amet, consectetur adipiscing elit. $3.299\times 10^{29}$ Aliquam ligula dui, blandit et
posuere eu, varius in erat. Nunc ornare tellus in dui tempus facilisis.
\end{document}
\myboxed
已配备一个可选参数来修改颜色(默认为black!5
或 5% 黑色):
\myboxed[<colour>]{<stuff>}
答案3
将深度和高度的两个值更改为您需要的值:
\documentclass[a5paper]{article}
\usepackage{mathtools}
\newsavebox\FBox
\def\Boxed#1{\sbox\FBox{%\fboxsep=1.5pt% may also be useful
$\boxed{#1}$}\rule[-1.2\dp\FBox]{0pt}{1.2\ht\FBox}\usebox\FBox}
\begin{document}
This is some long text This is some long text
This is some long text This is some long text
\Boxed{3.299\times10^{29}} This is some long text
This is some long text This is some long text This is some long text
This is some long text This is some \Boxed{3.299} long text
\end{document}
答案4
解决方案 1
减少框和文本之间的间隔:
\setlength\fboxsep{1pt}
\lipsum*[1]$\boxed{10^x}$\lipsum[2]
解决方案 2
减小方框公式的大小:
\lipsum*[1]$\footnotesize\boxed{10^x}$\lipsum[2]