環境でアンダースコアをアクティブにしながら、数式モードで元の使用を維持するにはどうすればよいでしょうか。

環境でアンダースコアをアクティブにしながら、数式モードで元の使用を維持するにはどうすればよいでしょうか。

私は書式設定のショートカットパッケージの作成を試しています。下線付きのテキスト範囲を区切るためにアンダースコアを使いたいのですが、次のようにします。

some text _underlined text_ more text

マクロを定義するには、アンダースコアをアクティブにするだけです

\catcode`_=\active%
\def_#1_{\underline{1}}%

これは明らかに数式モードでのアンダースコアの使用と衝突するため、実行可能ではありません。

両方を機能させることは可能ですか? 可能であれば、どのようにすればよいですか?

答え1

できますが、やらない方が良いです。本当に、下線を引くことはタイポグラフィではよくない習慣だと考えられているからというだけではありません。何も得られません。

\documentclass{article}

\def\ustextormath{\ifmmode\expandafter_\else\expandafter\usunderline\fi}

\begingroup\lccode`~=`_
\lowercase{\endgroup
  \def\usunderline#1~{\underline{#1}}
  \protected\def~{\ustextormath}
}

\AtBeginDocument{\catcode`\_=\active}

\begin{document}

\section{Some _underlined_ text and $a_1$}

some text _underlined text_ more text and $a_1$

\end{document}

ここに画像の説明を入力してください

答え2

こんばんは。ニーズに合うなら、ぜひ試してみてください。技術的には、私の例では、アンダースコアはタイプセット中のカテゴリ 8 のアクティブな文字です。数式モードで使用し、テキスト モードではグループ内の\sb閉じる条件\it( ) を使用しました。グループを離れると、 は自動的に に戻ります。\itshape\myconditionfalse

役に立つコメントとヒントをありがとう、egreg、alexis、FK82! 楽しかったです!

\catcode`\_=13% Or use \active; from category 8.
\def\false{false}\def\mycondition{false}%
\def_{\ifmmode\sb
  \else
    \ifx\mycondition\false
      \begingroup\def\mycondition{true}\it%\itshape for LaTeXists
        \else
      \endgroup
    \fi
  \fi}
Text _more of text_ ToDo $abc_i^2 math$ even more _text_ etc.
%\bye

ボーナス:これは、次の内容を含むワンライナーです\newif

\catcode`_=13\newif\ifmyif\myiffalse\def_{\ifmmode\sb\else\ifmyif\endgroup\else\begingroup\myiftrue\it\fi\fi}Text _more of text_ ToDo $abc_i^2 math$ even more _text_ etc.%\bye

ムウェ

関連情報