5 重新提问关于段落文本高亮显示的问题

发布于 2022-04-15 11:35:02

我现有下列代码

\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}

f21a86b78249232348ee6747b25e0ee.jpg
三个问题:(1)行首需加mbox{},否则报错,如何修改代码?(2)同一段的行与行之间不需要颜色填充;(3)行间公式不加$$也会报错,如何修改代码?

查看更多

关注者
0
被浏览
826
1 个回答
雾月
雾月 2022-04-15
这家伙很懒,什么也没写!

(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}

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览