ソウルパッケージの代替品ですか?

ソウルパッケージの代替品ですか?

コンテクスト

テキストをハイライトしたり、取り消したりしたい場合があります。これはsoul、パッケージまたはulemパッケージのいずれかを使用して行うことができます。トリックを使うただし、それほどうまく機能しません。

問題

私がソウルについて抱えている問題は、用語集、新しいコマンド、引用ではうまく動作しないということです。基本的に、テキスト全体を「\hl{}as it have been discussed」で囲むことはできません。他の場所. そして、回避策を講じる\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動作させるためのコツもあります。

編集: 2 番目の質問、つまり図やテキストなどを含む特定のセクションの背景色を変更する簡単な方法があるかどうかについては、それほど簡単ではありません。コンテンツを別の環境 (またはなどmdframed)にラップするアプローチを使用できますtcolorboxが、これは書式設定に影響し、特にフロートなどの特定のコマンド/要素が内部で制限される可能性があります。以下に、少なくとも単純なコンテンツで機能する に基づく例を示しましたtcolorbox。また、ページの背景を変更するために を使用して代替アプローチを追加しましたafterpage

ウレム

次のように独自のhlコマンドを定義します (cmp.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、この回答に従うことで問題を回避することもできます\soulregisterhttps://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}

tカラーボックス

いくつかのトリックを使えば、 work を使用してカスタムのハイライト環境を作成しtcolorbox、図やテキストなどを含む可能性のある特定のセクションの背景色を変更することができますtcolorbox。すべての余白を に設定した環境を使用して0から を復元すると\parindent、基本的にボックスの外側にあるテキストが表示されます。さらに、 パッケージを使用して配置オプションfloatを指定することにより、やHなどのフロート環境を 内に含めることができますが、実際にはフロートしません。私はMWE で取り消し線に使用しました。tablefigureulem

次の 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最後に、興味深いアプローチは、およびパッケージを使用してページの背景を部分的に着色することですafterpagehttps://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}

関連情報