靈魂包的替代品?

靈魂包的替代品?

情境

人們可能想要突出顯示某些文本,或刪除它。這可以透過包包來完成soul,也可以透過ulem包包來完成使用技巧,儘管效果不太好。

問題

我對 Soul 的問題是它在詞彙表、新命令或引文方面表現不佳。基本上,你不能將整個文本用一個\hl{}基本上,您不能像已經討論過的別處。並解決方法\mbox{\gls{toto}},甚至只是{\gls{toto}}解決問題。

例如,請參閱以下最小工作範例:

\documentclass{article}

\usepackage{soul}
\usepackage[acronym]{glossaries}
\usepackage{color}

\newacronym{co2}{CO$_2$}{carbon dioxide}
\newcommand{\etal}{\textit{et al.}}

\begin{document}
    
    Lorem ipsum \gls{co2} dolor by Toto \etal{}\cite{latexcompanion} sit amet, consectetur adipiscing elit.
Etiam tortor sapien, rutrum vitae lacinia sit amet, mollis ac est. Aenean tortor orci \gls{co2}.
    
    
    \begin{thebibliography}{9}
        \bibitem{latexcompanion} 
        Michel Goossens, Frank Mittelbach, and Alexander Samarin. 
        \textit{The \LaTeX\ Companion}. 
        Addison-Wesley, Reading, Massachusetts, 1993.
    \end{thebibliography}
    
\end{document}

編譯良好,同時:

    [...]
\begin{document}
    
    \hl{Lorem ipsum \gls{co2} dolor by Toto \etal{}\cite{latexcompanion} sit amet, consectetur adipiscing elit.
Etiam tortor sapien, rutrum vitae lacinia sit amet, mollis ac est. Aenean tortor orci \gls{co2}.}
    
    [...]

給了一堆錯誤訊息,部分修復:

    [...]
\begin{document}
    
    Lorem ipsum \mbox{\gls{co2}} \hl{dolor by Toto \mbox{\etal{}} {\cite{latexcompanion}} sit amet, consectetur adipiscing elit.
Etiam tortor sapien, rutrum vitae lacinia sit amet, mollis ac est. Aenean tortor orci \mbox{\gls{co2}}.}
    
    [...]

\mbox{\gls{co2}}如果我在命令中包含第一個,則會失敗\hl{}。也許是太長了?

問題

  • 到底是什麼soul\hl{}命令和\cite{},\etal{}或詞彙表條目之間的問題?
  • 如何解決這些問題? (所提出的解決方案僅部分有效)
  • 是否有一種替代方案允許類似\hl{}但可以包圍整個程式碼區塊,可能包含圖形、文字等。

我想出的最好的方法是使用彩盒(另請參閱這裡), 像這樣:

[...]
\usepackage{tcolorbox}
\tcbuselibrary{breakable}
[...]
    \begin{tcolorbox}[breakable,notitle,boxrule=0pt,colback=yellow,colframe=yellow]
    
    Lorem ipsum \gls{co2} dolor by Toto \etal{}\cite{latexcompanion} sit amet, consectetur adipiscing elit.
Etiam tortor sapien, rutrum vitae lacinia sit amet, mollis ac est. Aenean tortor orci \gls{co2}.
    
    \end{tcolorbox}
[...]

但它改變了文字格式......


截至 2023 年 6 月更新

如果您使用 LuaLaTeX 進行編譯,您只需將以下內容新增到您的序言中:

\usepackage{luacolor}
\usepackage[soul]{lua-ul}

然後\hl{Lorem ipsum...}按照說明正常使用這裡\enquote與下面的一些解決方案相反,該方法具有魯棒性的優點。

答案1

ulem我想你可以使用這個包來實現你想要的。做事還有一個竅門soul

編輯:關於你的第二個問題是否有一種簡單的方法來更改可能包含圖形、文字等的精確部分的背景顏色,這並不容易。您可以使用將內容包裝到另一個環境中的方法(例如mdframedtcolorbox),但這可能會影響您的格式,並且可能會限制內部的某些命令/元素,尤其是浮動。我在tcolorbox下面提供了一個至少適用於簡單內容的範例。另外,我添加了一種用於afterpage更改頁面背景的替代方法。

烏萊姆

定義你自己的hl像這樣https://tex.stackexchange.com/a/48549/223749):

\usepackage[normalem]{ulem}
\newcommand\hl{\bgroup\markoverwith
    {\textcolor{yellow}{\rule[-.5ex]{.1pt}{2.5ex}}}\ULon}

這樣,您的 MWE 就可以正常編譯了。 (看一下https://tex.stackexchange.com/a/254337/223749對於可能出現的問題)。使用sout命令ulem還可以刪除 MWE 中的相同文本,而不會出現錯誤。

完整的 MWE(您的 MWE 缺少\makeglossaries命令):

\documentclass{article}

\usepackage[normalem]{ulem}
\newcommand\hl{\bgroup\markoverwith
  {\textcolor{yellow}{\rule[-.5ex]{.1pt}{2.5ex}}}\ULon}
\usepackage[acronym]{glossaries}
\usepackage{color}

\newacronym{co2}{CO$_2$}{carbon dioxide}
\newcommand{\etal}{\textit{et al.}}
\makeglossaries

\begin{document}

\hl{Lorem ipsum \gls{co2} dolor by Toto \etal{}\cite{latexcompanion} sit amet, consectetur adipiscing elit.  Etiam tortor sapien, rutrum vitae lacinia sit amet, mollis ac est. Aenean tortor orci \gls{co2}.}
\sout{Lorem ipsum \gls{co2} dolor by Toto \etal{}\cite{latexcompanion} sit amet, consectetur adipiscing elit.  Etiam tortor sapien, rutrum vitae lacinia sit amet, mollis ac est. Aenean tortor orci \gls{co2}.}
    
    
    \begin{thebibliography}{9}
        \bibitem{latexcompanion} 
        Michel Goossens, Frank Mittelbach, and Alexander Samarin. 
        \textit{The \LaTeX\ Companion}. 
        Addison-Wesley, Reading, Massachusetts, 1993.
    \end{thebibliography}
    
\end{document}

靈魂

如果您想堅持使用soul,還有一個解決方法可以使用\soulregister以下答案來解決您的問題(https://tex.stackexchange.com/a/139500/223749)。這也提供了刪除線的可能性。

完整的 MWE:

\documentclass{article}

\usepackage{soul}
\soulregister\cite7
\soulregister\gls7
\soulregister\etal7

\usepackage[acronym]{glossaries}
\usepackage{color}

\newacronym{co2}{CO$_2$}{carbon dioxide}
\newcommand{\etal}{\textit{et al.}}
\makeglossaries

\begin{document}

\hl{Lorem ipsum \gls{co2} dolor by Toto \etal{}\cite{latexcompanion} sit amet, consectetur adipiscing elit.  Etiam tortor sapien, rutrum vitae lacinia sit amet, mollis ac est. Aenean tortor orci \gls{co2}.}
\st{Lorem ipsum \gls{co2} dolor by Toto \etal{}\cite{latexcompanion} sit amet, consectetur adipiscing elit.  Etiam tortor sapien, rutrum vitae lacinia sit amet, mollis ac est. Aenean tortor orci \gls{co2}.}
    
    
    \begin{thebibliography}{9}
        \bibitem{latexcompanion} 
        Michel Goossens, Frank Mittelbach, and Alexander Samarin. 
        \textit{The \LaTeX\ Companion}. 
        Addison-Wesley, Reading, Massachusetts, 1993.
    \end{thebibliography}
    
\end{document}

顏色盒

透過一些技巧,您可以使用工作創建自訂突出顯示環境,該環境tcolorbox可以更改可能包含圖形、文字等的精確部分的背景顏色。就像它在盒子外面一樣。此外,透過使用套件並指定放置選項,您可以包含浮動環境,例如或inside - 儘管它們實際上不會浮動。我在 MWE 中使用了刪除線。tcolorbox0\parindentfloatHtablefigureulem

以下 MWE 包含一個圖表和一個表,以及一個分頁符號。

\documentclass{article}

\usepackage{mwe}

\usepackage[acronym]{glossaries}
\usepackage{color}

\usepackage{float}
\usepackage[normalem]{ulem}

\newlength\defaultparindent
\AtBeginDocument{\setlength\defaultparindent{\parindent}}

\usepackage[breakable]{tcolorbox}
\newenvironment{highlighttcb}%
  {\begin{tcolorbox}[colback=yellow,
                     colframe=white,
                     boxsep=0pt,
                     left=0pt,
                     right=0pt,
                     top=0pt,
                     bottom=0pt,
                     center, 
                     valign=top, 
                     before skip=0pt, 
                     after skip=0pt,
                     width=\textwidth,
                     breakable,
                     boxrule=0pt,
                     frame empty,
                     sharp corners
                 ]
             \setlength{\parindent}{\defaultparindent}
             }%
  {\end{tcolorbox}}


\newacronym{co2}{CO$_2$}{carbon dioxide}
\newcommand{\etal}{\textit{et al.}}
\makeglossaries

\begin{document}
    \blindtext
    \blindtext
    \blindtext
    \blindtext
    \blindtext

\begin{highlighttcb}%
    Lorem ipsum \gls{co2} dolor by Toto \etal{}\cite{latexcompanion} sit amet, consectetur adipiscing elit.  Etiam tortor sapien, rutrum vitae lacinia sit amet, mollis ac est. Aenean tortor orci \gls{co2}.

    \sout{Lorem ipsum \gls{co2} dolor by Toto \etal{}\cite{latexcompanion} sit amet, consectetur adipiscing elit.  Etiam tortor sapien, rutrum vitae lacinia sit amet, mollis ac est. Aenean tortor orci \gls{co2}.}

    \begin{figure}[H]
        \includegraphics[width=.4\textwidth]{example-image-a}\hfill
        \includegraphics[width=.4\textwidth]{example-image-b}
        \caption{This is a figure caption within tcolorbox.}
    \end{figure}

    \blindtext
    \blindtext
    \blindtext
    \blindtext
    \begin{table}[H]
        \centering
        \begin{tabular}{lcr}
        a & b & c \\
        a & b & c \\
        a & b & c \\
        \end{tabular}
        \caption{This is a table caption within tcolorbox.}
    \end{table}
    \blindtext

\end{highlighttcb}

\begin{thebibliography}{9}
    \bibitem{latexcompanion} 
    Michel Goossens, Frank Mittelbach, and Alexander Samarin. 
    \textit{The \LaTeX\ Companion}. 
    Addison-Wesley, Reading, Massachusetts, 1993.
\end{thebibliography}
    
\end{document}

基於 tcolorbox 的自訂反白顯示環境

頁面顏色/後頁

color最後,一個有趣的方法是使用和套件對頁面背景進行部分著色afterpage(取自https://tex.stackexchange.com/a/237427/223749)。這樣做的優點是您根本不需要更改內容並且格式不會受到影響。一個可能的缺點(取決於您的目的)可能是突出顯示也將包括左側和右側邊距。另外,如果您的突出顯示環境中有浮動並且它們浮出環境,則它們不會突出顯示。

完整的 MWE,包括圖形和分頁符號:

\documentclass{article}

\usepackage{mwe}

\usepackage[acronym]{glossaries}
\usepackage{color}

\usepackage[normalem]{ulem}
\usepackage{afterpage}

\makeatletter
% Macro \changepagecolor has the same syntax as \pagecolor or \color
% with an optional argument and a mandatory argument.
\newcommand*{\changepagecolor}{%
  \@ifnextchar[\@changepagecolor@i\@changepagecolor@ii
}
% Case: \changepagecolor[...]{...}
\def\@changepagecolor@i[#1]#2{%
  \@changepagecolor@do{[{#1}]{#2}}%
}
% Case: \changepagecolor{...}
\newcommand*{\@changepagecolor@ii}[1]{%
  \@changepagecolor@do{{#1}}%
}
\newcommand*{\@changepagecolor@do}[1]{%
  % Fill the remaining space with a colored rule
  \begingroup
    \offinterlineskip
    \hbox to 0pt{%
      \kern-\paperwidth
      \vtop to 0pt{%
        \color#1%
        \hrule width 2\paperwidth height \paperheight
        \vss
      }%
      \hss
    }%
  \endgroup
  % Set page color for the next page
  \afterpage{\pagecolor#1}%
}
\makeatother

\newenvironment{highlight}%
  {\changepagecolor{yellow}}%
  {\changepagecolor{white}}


\newacronym{co2}{CO$_2$}{carbon dioxide}
\newcommand{\etal}{\textit{et al.}}
\makeglossaries

\begin{document}
\blindtext
\blindtext
\blindtext
\blindtext

\begin{highlight}
Lorem ipsum \gls{co2} dolor by Toto \etal{}\cite{latexcompanion} sit amet, consectetur adipiscing elit.  Etiam tortor sapien, rutrum vitae lacinia sit amet, mollis ac est. Aenean tortor orci \gls{co2}.

\sout{Lorem ipsum \gls{co2} dolor by Toto \etal{}\cite{latexcompanion} sit amet, consectetur adipiscing elit.  Etiam tortor sapien, rutrum vitae lacinia sit amet, mollis ac est. Aenean tortor orci \gls{co2}.}

\blindtext

\begin{figure}[!h]
    \includegraphics[width=.4\textwidth]{example-image-a}\hfill
    \includegraphics[width=.4\textwidth]{example-image-b}
    \caption{This is a figure caption within the custom highlight environment}
\end{figure}
\end{highlight}

\blindtext

\begin{thebibliography}{9}
    \bibitem{latexcompanion} 
    Michel Goossens, Frank Mittelbach, and Alexander Samarin. 
    \textit{The \LaTeX\ Companion}. 
    Addison-Wesley, Reading, Massachusetts, 1993.
\end{thebibliography}
    
\end{document}

使用 pagecolor/afterpage 來反白顯示

答案2

另一種方法是使用proofread包和\hilite命令。但是,您必須\protect在 之前使用該命令\gls並在 之後立即關閉大括號\gls。然後\hilite再次使用突出顯示文字的其餘部分。

\documentclass{article}
\usepackage{soulutf8} % Only necessary if there are accented characters inside the curly braces
\usepackage{proofread}
\usepackage[xindy]{glossaries}
\makeglossaries
    
\newglossaryentry{euismod}{
        name={euismod},
        description={euismod sodales, sollicitudin vel, wisi. Morbi auctor lorem non justo.}
    }
    
    
\begin{document}
                
    Curabitur auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue eu, \hilite{accumsan eleifend.
        
    Nam dui ligula, fringilla a, \protect\gls{euismod}}\hilite{ sodales, sollicitudin vel, wisi. Morbi auctor lorem non justo}. Nam lacus libero, pretium at. 
                
\printglossary  

\end{document}

相關內容