正如标题所说,我想突出显示文档中的$...$
、\(...\)
、的所有内容。\begin{math}...\end{math}
我的最后一次尝试,但问得不太好,是:
不幸的是,在最终测试了所提出的解决方案之后,我发现:
- 当数学变得更高时它就会失败 - 例如使用
\frac
或\displaystyle
。 - 当数学延伸时它就会失败
\textwidth
——突出显示的目的之一就是发现超出范围的线条。 - 需要用一对单独的标记来标记每个方程式,这很麻烦。
我尝试修复一些问题,但却完全迷失在代码中。
更新
@jfbu 的答案看起来相当令人满意。目前我能想到的唯一未涵盖的测试案例是
Finally, while display equations look good for a page of samples,
the ability to mix math and text
\(\displaystyle \frac{1}{(\sqrt{\phi \sqrt{5}}-\phi) e^{\frac25 \pi}} =
1+\frac{e^{-2\pi}} {1+\frac{e^{-4\pi}} {1+\frac{e^{-6\pi}}
{1+\frac{e^{-8\pi}} {1+\ldots} } } }\)
The amssymb package might be omissible for documents whose math.
我想恒定的高度会给人更好的感觉。
下次更新
正如更新后的答案中所述,混合分布在几行上的内容高度会产生不必要的结果,例如
Finally, while display equations look good for a page of samples,
the ability to mix math and text \(\displaystyle ABC_{1234} +
ABC_{1234} - ABC_{1234} + ABC_{1234} - ABC_{1234} =
\frac{1}{(\sqrt{\phi \sqrt{5}}-\phi) e^{\frac25 \pi}} =
1+\frac{e^{-2\pi}} {1+\frac{e^{-4\pi}} {1+\frac{e^{-6\pi}}
{1+\frac{e^{-8\pi}} {1+\ldots} } } }\).
The amssymb package might be omissible for certain documents.
它将每行视为高度
是否可以使突出显示行具有感知能力?
我希望我没有越界,因为这里已经完成了伟大的工作。无论如何,这一步可以让它变得完美无缺。
答案1
努力工作带来了更好的结果。但这并不严格,TeX
它利用了\pdfsavepos
通过进球线包裹。
因此,命令\Highlight
可用于为段落中从 A 到 B 的部分提供彩色背景,并允许换行。虽然 OP 的问题是关于内联数学,但最终的答案是与数学没有具体联系。从理论上讲,该宏除了添加彩色背景外,不会改变段落的布局。
为了避免这些颜色条覆盖其他地方的降部,用户可以 \strut's
在宏参数的起点和终点之前和之后添加 ,这不是自动完成的(但至少可以作为一个选项)。如果要在段落开头使用宏,则应在宏前面加上\indent
或\noindent
。
该宏无法处理突出显示的材料中出现的分页符。并且它不能同时用于多个段落。
材料排版了三次,前两次在临时内部,\vboxes
但这会影响\LaTeX
计数器,因为它们是全局的。一些更精细的宏版本会处理这些事情。
如果没有 pdftex,整个事情就无法进行\pdfsavepos
如果没有 pdftex包,进球线。
\documentclass{article}
\usepackage{color}
\usepackage{linegoal} % http://ctan.org/pkg/linegoal
% debugging
% \showboxdepth\maxdimen
% \showboxbreadth\maxdimen
% \errorcontextlines 5
\definecolor{SkyBlue}{cmyk}{0.62,0,0.12,0}
\definecolor{BlueGreen}{cmyk}{0.85,0,0.33,0}
\definecolor{Thistle}{cmyk}{0.12,0.59,0,0}
\newbox\HighlightPiece
\newdimen\FirstHeight
\newdimen\FirstDepth
\newdimen\LastHeight
\newdimen\LastDepth
\newdimen\TotalHeight
\newdimen\TotalDepth
\newdimen\LineIndent
\newdimen\LineGoal
\newif\ifOnePiece
\makeatletter
\newcommand\HLGetDimensions {%
\setbox\HighlightPiece\lastbox
\unskip\unpenalty
\ifdim\wd\HighlightPiece<\linewidth
\global\OnePiecetrue
\else
\global\OnePiecefalse
\fi
\ifOnePiece
\else
\global\LastHeight\ht\HighlightPiece
\global\LastDepth\dp\HighlightPiece
\loop %
\setbox\HighlightPiece\lastbox
\unskip\unpenalty
\ifvoid\HighlightPiece\else
\ifdim\wd\HighlightPiece<\linewidth
\global\FirstHeight\ht\HighlightPiece
\global\FirstDepth\dp\HighlightPiece
\fi
\repeat
\fi
}
\newcommand\Highlight [2][yellow]
{%
\LineGoal\linegoal % http://ctan.org/pkg/linegoal
\LineIndent=\dimexpr\linewidth-\LineGoal\relax
\vbox{%
\hfuzz\maxdimen
\hangindent\LineIndent
\hangafter\m@ne
\noindent #2\endgraf
\HLGetDimensions
}%
\ifOnePiece
\begingroup \fboxsep\z@
\colorbox{#1}{#2}%
\endgroup
\else
\setbox\z@\vbox{%
\hfuzz\maxdimen
\hangindent\LineIndent
\hangafter\m@ne
\noindent #2\endgraf }%
\TotalHeight\ht\z@
\TotalDepth\dp\z@
\advance\TotalHeight\TotalDepth
\advance\TotalHeight-\FirstHeight
\advance\TotalHeight-\FirstDepth
\begingroup % et pas \color@begingroup, cf plus bas!
\color{#1}%
\rlap{\hbox{\vrule\@height\FirstHeight
\@depth\FirstDepth
\@width\LineGoal}}%
\setbox\z@\vbox{\moveleft\LineIndent
\hbox{\vrule
\@height\TotalHeight
\@depth\z@
\@width\linewidth}}%
\ht\z@\z@\dp\z@-\FirstDepth\wd\z@\z@
\lower\FirstDepth\box\z@
\endgroup % \color@endgroup fait basculer en mode vertical!!
#2%
\LineGoal\linegoal
\rlap{\hbox{\begingroup\color{white}%
\vrule\@height\LastHeight
\@depth\LastDepth
\@width\dimexpr\LineGoal+1pt\relax
\endgroup}}% this 1pt could be a user hook dimen.
\fi
}%
\makeatother
\renewcommand\([1][yellow]{\GatherAndHighlight {#1}}
\def\GatherAndHighlight #1#2\){\Highlight[#1]{$#2$}}
\begin{document}
Here is some test \(\displaystyle 1+\frac{e^{-2\pi}} {1+\frac{e^{-4\pi}}
{1+\frac{e^{-6\pi}}
{1+\frac{e^{-8\pi}} {1+\ldots}}}}=\displaystyle
\frac{1}{(\sqrt{\phi \sqrt{5}}-\phi) e^{\frac25 \pi}}\) inline.
Here is some fundamental (inline) new equation governing everything in the
universe: \Highlight[SkyBlue]{$ABC_{1234} = ABC_{1234} + ABC_{1234} - ABC_{1234} +
ABC_{1234} -
ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} -
ABC_{1234} + ABC_{1234} - ABC_{1234}$}. Pretty impressive, no?
Here is some fundamental (inline) new equation governing everything in the
universe: \([green]ABC_{1234} = A_{1} + AB_{12} - ABC_{123} + A_{1} = {\displaystyle
\frac{1}{(\sqrt{\phi \sqrt{5}}-\phi) e^{\frac25 \pi}}} = AB_{12} - ABC_{123} + A_{1} + AB_{12} - ABC_{123} + AB_{12} -
ABC_{123} + A_{1} = ABC_{1234} = A_{1} + AB_{12} - ABC_{123} + A_{1} = {\displaystyle
\frac{1}{(\sqrt{\phi \sqrt{5}}-\phi) e^{\frac25 \pi}}} = AB_{12} - ABC_{123} + A_{1} + AB_{12} - ABC_{123} + AB_{12} -
ABC_{123} + A_{1} \). Pretty impressive, no?
I worked (very) hard and it seems to cope fairly well (but \emph{do not
forget to compile many times, at least twice after each change}):
\(ABC_{1234} = A_{1} + AB_{12} - ABC_{123} + A_{1} = {\displaystyle
\frac{1}{(\sqrt{\phi \sqrt{5}}-\phi) e^{\frac25 \pi}} =
1+\frac{e^{-2\pi}} {1+\frac{e^{-4\pi}} {1+\frac{e^{-6\pi}}
{1+\frac{e^{-8\pi}} {1+\ldots}}}}} = AB_{12} - ABC_{123} + A_{1} +
AB_{12} - ABC_{123} + AB_{12} - ABC_{123} + A_{1} \), although not
completely perfectly (adding struts could help). Let me take this
opportunity to say that \Highlight[red]{\strut I will NOT handle the case of
page breaks occurring mid-way through inline material!\strut}
Actually, as we saw this powerful and beautiful macro
\texttt{\string\Highlight} is absolutely not \Highlight[Thistle]{limited to
inline math, it may contain any sort of breakable material,
$ABC_{1234} = A_{1} + ABCD_{123456} - ABC_{123} +
A_{1} = {\displaystyle \frac{1}{(\sqrt{\phi \sqrt{5}}-\phi)
e^{\frac25 \pi}}} = AB_{12} - ABC_{123} + A_{1} + AB_{12} -
ABC_{123} + AB_{12} - ABC_{123}$ including math within text.}
If you see very dim thinny horizontal lines, it may be an artefact of the
bad rendering of colored boxes in some pdf viewers!
The impact on actual layout of the paragraph is supposed to be
non-existent, we can see here with a case of slight overfull that line
breaks are not modified.
Things may be a bit full. \(ABC_{1234} = A_{1} + AB_{12} - ABC_{123} +
A_{1} = {\displaystyle
\frac{1}{(\sqrt{\phi \sqrt{5}}-\phi) e^{\frac25 \pi}} =
1+\frac{e^{-2\pi}} {1+\frac{e^{-4\pi}} {1+\frac{e^{-6\pi}}
{1+\frac{e^{-8\pi}} {1+\ldots}}}}} = AB_{12} - ABC_{123} + A_{1} +
AB_{12} - ABC_{123} + AB_{12} -
ABC_{123} + A_{1} \).
Things may be a bit full. $ABC_{1234} = A_{1} + AB_{12} - ABC_{123} +
A_{1} = {\displaystyle
\frac{1}{(\sqrt{\phi \sqrt{5}}-\phi) e^{\frac25 \pi}} =
1+\frac{e^{-2\pi}} {1+\frac{e^{-4\pi}} {1+\frac{e^{-6\pi}}
{1+\frac{e^{-8\pi}} {1+\ldots}}}}} = AB_{12} - ABC_{123} + A_{1} +
AB_{12} - ABC_{123} + AB_{12} -
ABC_{123} + A_{1} $.
\end{document}
这是我之前提议的演变。现在突出显示将尝试计算第一个最大的高度和深度;当然,这意味着如果材料分线并且其中一块非常高/深,这会影响一切,无论如何,在这种情况下,显示数学将是更好的选择。
至于评论中提到的“突出显示环境”,如果这指的是显示环境,则需要其他方法。我猜是将环境包装在另一个framed
或mdframed
类型中。这里的方法严格用于内联数学。实际上,它还可以用于显示单词中可能的连字符点(实际上,该方法受到 Tex by Topic 5.9.6 使用 \lastbox 剖析段落的启发,texdoc topic
在包中也以更复杂的形式使用soul
)。
编辑:全局\sbox
现在使用\box1
而不是\HighlightBox
,因为我记得本地和全局分配不应该混合,尽管在这里我不知道这在保存堆栈影响方面是否对这个代码有任何重要性。
\documentclass{article}
\usepackage{color}
\definecolor{SkyBlue}{cmyk}{0.62,0,0.12,0}
\definecolor{BlueGreen}{cmyk}{0.85,0,0.33,0}
\definecolor{Thistle}{cmyk}{0.12,0.59,0,0}
% BOXES FOR THE DECORATION
\newbox\HighlightPiece
\newbox\HighlightBox
\newbox\HighlightStrutBox
\makeatletter
\def\Highlight@ #1{%
% technique roughly inspired (but now with a \loop) from
% TeX by Topic 5.9.6 Dissecting paragraphs with \lastbox
\loop % FIRST WE LOOP OVER THE CONTENT TO GET MAXIMAL HEIGHT AND DEPTH
\setbox\HighlightPiece\lastbox
\ifvoid\HighlightPiece\else
\unskip\unpenalty
\setbox\HighlightStrutBox\hbox{\unhbox\HighlightStrutBox
\vrule\@height\ht\HighlightPiece
\@depth\dp\HighlightPiece
\@width\z@ }%
\setbox\HighlightBox\vbox{\box\HighlightPiece\unvbox\HighlightBox}%
\repeat
\unvbox\HighlightBox
\loop % WE RE-LOOP TO BUILD A HORIZONTAL LIST WITH SUITABLE DECORATION
\setbox\HighlightPiece\lastbox
\ifvoid\HighlightPiece\else
\unskip\unpenalty
\setbox\HighlightBox
\hbox{\colorbox{#1}{\color{black}%
\unhcopy\HighlightStrutBox
\unhbox\HighlightPiece}%
\allowbreak
\leaders\hrule \@height \ht\HighlightStrutBox
\@depth \dp\HighlightStrutBox
% I would like to have some more clever choice here
% than this 2pt amount of stretchability to reproduce
% what would have happened without all our fiddling
\hskip 0pt plus 2pt\relax %
\unhbox\HighlightBox}%
\repeat}
\newcommand\Highlight [2][yellow]
{\vbox{%
\setbox\HighlightBox\vbox{}%
\setbox\HighlightStrutBox\hbox{\unhcopy\strutbox}%
% TO LEAVE INTERLINE WHITE SPACE REPLACE PREVIOUS LINE BY
% \setbox\HighlightStrutBox\hbox{}%
\hfuzz\maxdimen % temporarily do not report overfull boxes
\hsize 1sp % force the breaking in unbreakable pieces
\parindent\z@
\leftskip\z@skip
\rightskip\z@skip
\parfillskip\@flushglue
\fboxsep \z@ % \colorbox uses \fboxsep !!
\color{#1}% set color for future interboxes stretchable rules
\leavevmode\hskip\z@skip % done for the "hyphenation variant"
#2\endgraf % paragraph builder
\Highlight@{#1}% dissect the paragraph into boxes.
\global\sbox\@ne % Color Protection with \sbox
{\unhbox\HighlightBox}}%
\unhbox\@ne % re-insert the colorized boxes into the current
% (real) paragraph.
}%
\makeatother
% We could make this in a command like \TurnOnInlineHighlight
% and \TurnOffInlineHighlight would then restore the normal definitions.
\renewcommand\([1][yellow]{\GatherAndHighlight {#1}}
\def\GatherAndHighlight #1#2\){\Highlight[#1]{$#2$}}
\begin{document}
Here is some fundamental (inline) new equation governing everything in the
universe: $ABC_{1234} = ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} -
ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} -
ABC_{1234} + ABC_{1234} - ABC_{1234}$. Pretty impressive, no?
Here is some fundamental (inline) new equation governing everything in the
universe: \Highlight[SkyBlue]{$ABC_{1234} = ABC_{1234} + ABC_{1234} - ABC_{1234} +
ABC_{1234} -
ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} -
ABC_{1234} + ABC_{1234} - ABC_{1234}$}. Pretty impressive, no?
Here is some fundamental (inline) new equation governing everything in the
universe: \(ABC_{1234} = ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} -
ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} -
ABC_{1234} + ABC_{1234} - ABC_{1234}\). Pretty impressive, no?
Here is some fundamental (inline) new equation governing everything in the
universe: \([green]ABC_{1234} = ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} -
ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} -
ABC_{1234} + ABC_{1234} - ABC_{1234}\). Pretty impressive, no?
Finally, while display equations look good for a page of samples,
the ability to mix math and text \(\displaystyle
\frac{1}{(\sqrt{\phi \sqrt{5}}-\phi) e^{\frac25 \pi}} =
1+\frac{e^{-2\pi}} {1+\frac{e^{-4\pi}} {1+\frac{e^{-6\pi}}
{1+\frac{e^{-8\pi}} {1+\ldots} } } }\).
The amssymb package might be omissible for certain documents.
\Highlight{Brave souls will be tempted to apply the highlighting to pieces of
text. As we can see here, the result is a bit curious.} On the other hand this
is quite ok for just displaying the hyphenation points of a single word: for
example \Highlight[Thistle]{displaying}.
\end{document}
这是早先的提议:
\documentclass{article}
\usepackage{color}
\newbox\HighlightPiece
\newbox\HighlightAll
\def\colorizeboxes #1{%
\setbox\HighlightPiece\lastbox
\ifvoid\HighlightPiece\else
\unskip\unpenalty
\setbox\HighlightAll
\hbox{\colorbox{#1}{\color{black}\strut\unhbox\HighlightPiece}%
\allowbreak
\leaders\hrule height \ht\strutbox depth \dp\strutbox
\hskip 0pt plus 2pt
\unhbox\HighlightAll}%
\colorizeboxes {#1}%
\fi}
\newcommand\Highlight [2][yellow]
{\vbox{\setbox\HighlightAll\hbox{}\parindent0pt
\rightskip0pt plus 1fil
\fboxsep 0pt
\offinterlineskip
\hsize1pt
\color{#1}%
#2\endgraf\colorizeboxes{#1}%
\global\sbox\HighlightAll{\unhbox\HighlightAll}}%
\unhbox\HighlightAll}%
\renewcommand\([1][yellow]{\GatherAndHighlight {#1}}
\def\GatherAndHighlight #1#2\){\Highlight[#1]{$#2$}}
\begin{document}
Here is some fundamental (inline) new equation governing everything in the
universe: $ABC_{1234} = ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} -
ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} -
ABC_{1234} + ABC_{1234} - ABC_{1234}$. Pretty impressive, no?
Here is some fundamental (inline) new equation governing everything in the
universe: \Highlight{$ABC_{1234} = ABC_{1234} + ABC_{1234} - ABC_{1234} +
ABC_{1234} -
ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} -
ABC_{1234} + ABC_{1234} - ABC_{1234}$.} Pretty impressive, no?
Here is some fundamental (inline) new equation governing everything in the
universe: \(ABC_{1234} = ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} -
ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} -
ABC_{1234} + ABC_{1234} - ABC_{1234}\). Pretty impressive, no?
Here is some fundamental (inline) new equation governing everything in the
universe: \([green]ABC_{1234} = ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} -
ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} - ABC_{1234} + ABC_{1234} -
ABC_{1234} + ABC_{1234} - ABC_{1234}\). Pretty impressive, no?
\end{document}
输出:
答案2
另一个简单的解决方案是改变描边颜色。
\documentclass[preview,border=12pt]{standalone}
\usepackage{amsmath,xcolor}
\everymath{\color{red}}
\begin{document}
Don't be lukewarm! If you cannot be the best, be the worst!
This is a quadratic equation $ax^2+bx+c=0$ where $a\ne0$.
Have you learnt it before?
\end{document}