我现有下列代码
\documentclass[fontset=windows]{ctexart}
\usepackage{color}
\usepackage{linegoal}
\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!!
\mbox{}#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
\begin{document}
\mbox{}\Highlight{文字文字文字文字文字文字文字文文字文字文字文文字文字文字文字文字文字文,}文字文文字文
\mbox{}\Highlight{测试测试测试测$f(x)$试测试测试测试测试}
\[
\Highlight{$\displaystyle\int f(x),x\in A$}
\]
\end{document}
三个问题:(1)行首需加mbox{},否则报错,如何修改代码?(2)同一段的行与行之间不需要颜色填充;(3)行间公式不加$$也会报错,如何修改代码?
(1)添加 \leavevmode
。(2)没办法。(3)模拟 amsmath
宏包的 \boxed
。
这种东西不适合用 LaTeX 制作。
缺陷:数学公式不能跨行,align
等对齐环境下存在问题,文字不能分页等问题。
\documentclass[fontset=windows]{ctexart}
\usepackage{amsmath,mathtools}
\usepackage{color}
\usepackage{linegoal}
\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\Highlighttext [2][yellow]
{%
\leavevmode
\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!!
\mbox{}#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
}%
\newcommand\Highlight@dmath[2][yellow]{{\fboxsep=0pt\fboxrule=0pt\fcolorbox{#1}{#1}{\m@th$\displaystyle#2$}}}
\newcommand\Highlight{\ifmmode\expandafter\Highlight@dmath
\else\expandafter\Highlighttext\fi}
\makeatother
\begin{document}
\Highlight{文字文字文字文字文字文字文字文文字文字文字文文字文字文字文字文字文字文,}文字文文字文
\Highlight{测试测试测试测$f(x)$试测试测试测试测试}测试
\[
\Highlight{\int f(x),x\in A}
\]
\begin{align}
a&=b \\
c&=d \\
k&=\Highlight{\int_a^c dx} \\
\Aboxed{k&=\int_a^c dx}
\end{align}
\end{document}
有没有其他方案能让(2)解决?