환경에서 밑줄을 활성화하되 수학 모드에서는 원래 사용을 유지하려면 어떻게 해야 합니까?

환경에서 밑줄을 활성화하되 수학 모드에서는 원래 사용을 유지하려면 어떻게 해야 합니까?

형식화 속기 패키지를 만드는 실험을 하고 있습니다. 밑줄을 사용하여 밑줄이 그어진 텍스트 범위를 구분하고 싶습니다.

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

MWE

관련 정보