수학 모드의 경우 $와 같이 texttt 모드로 들어가는 쉬운 방법이 있습니까?

Aug 19 2020

인덱스 i와 같이 수학 변수를 참조하고 싶을 때 $i$쓰는 대신 달러 (예 :)로 둘러 쌉니다 \begin{math}i\end{math}.

texttt모드에 대한 유사한 옵션 ( 예 : §i§교체)이 \texttt{i}있습니까?

답변

27 egreg Aug 19 2020 at 23:04

§동일한 첫 번째 바이트를 사용하는 다른 문자에 영향을주지 않고 구분 기호로 사용할 수 있습니다 .

\documentclass{article}
\usepackage{newunicodechar}

\newunicodechar{§}{\makeabbreviationtt}
\def\makeabbreviationtt#1§{\texttt{#1}}

\begin{document}

This is §monospaced§. This doesn't affect ©, ¶
and other similar UTF-8 characters.

You can also do §¶§

\end{document}

22 DavidCarlisle Aug 19 2020 at 18:27

아니 \texttt하지만에 대한 \verb기본 배포 포함 shortvrb당신이 속기를 정의 할 수 있습니다 패키지를

\documentclass{article}
\usepackage{shortvrb}
\MakeShortVerb|


\begin{document}
  aaaa |z|  aaa
\end{document}
15 StevenB.Segletes Aug 19 2020 at 18:20
\documentclass{article}
\def§#1§{\texttt{#1}}
\begin{document}
roman §tt text§ back to roman
\end{document}

그러나 David가 주석에서 지적했듯이은 §단일 바이트 문자가 아니라 UTF-8 확장 (멀티 바이트) 문자이기 때문에이 방법은 동일한 접두사로 시작하는 UTF-8 문자를 모두 지 웁니다. 해당 문자를 사용하면 오류가 발생합니다 (예 :) ©.

따라서 이러한 유형의 솔루션을 원한다면 구분 기호를 단일 바이트 ASCII 문자로 선택하고 활성화하는 것이 좋습니다.

\documentclass{article}
\catcode`|=\active
\def|#1|{\texttt{#1}}
\begin{document}
roman |tt text| back to roman
\end{document}

여기서 단점은 |일반 입력 문자로 의 사용을 잃는다는 것입니다 . 따라서 멋지게 정의에 이스케이프를 구축하여 ||함께 |조판 될 단일 을 에코 할 수 있습니다.

\documentclass{article}
\usepackage[T1]{fontenc}
\let\svvert|
\catcode`|=\active
\def|#1|{\ifx\relax#1\relax\expandafter\svvert\else\texttt{#1}\fi}
\begin{document}
roman |tt text| back to roman || or $y =||x||$ and |then back to texttt|.
\end{document}