Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm experimenting with creating a formatting shorthand package. I want to use the underscore to delimit an underlined text span, like so

some text _underlined text_ more text

Simply making the underscore active defining a macro

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

is not viable since this obviously collides with the use of the underscore in math mode.

Is it possible to have both work? If so, how?

share|improve this question
    
The answer to your question title is yes. The answer to your question body is move complicated. Could you re-word your title to match your document (something more specific)? –  Werner Mar 17 at 19:56
    
@Werner I changed the title to be more specific. –  FK82 Mar 17 at 20:26
add comment

2 Answers 2

up vote 7 down vote accepted

You can do it, but it's better not to. Really, and not only because underlining is considered bad practice in typography. You gain nothing.

\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}

enter image description here

share|improve this answer
    
What do you mean by "You gain nothing."? –  FK82 Mar 17 at 21:20
2  
@FK82 You clutter your text with strange characters that don't really show the logical markup. –  egreg Mar 17 at 21:24
    
Ok. Sure. You got a point. –  FK82 Mar 17 at 21:34
add comment

Good evening, please try this if it fits your needs. Technically, underscore is an active character from category 8 in my example during typesetting. I used \sb in the math mode and condition for closing \it (\itshape) in a group in the text mode. The \mycondition is returning to false automatically when leaving a group.

Thank you for useful comments and tips egreg, alexis and FK82! It was fun!

\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

Bonus: This is a one-liner with \newif involved:

\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

share|improve this answer
2  
The usual category code of _ is 8, not 13. –  egreg Mar 17 at 20:57
1  
Oh, and changing the category code of ^^J is definitely not a good idea. Use \sb which is a perfect substitute for the usual _ as a subscript prefix. –  egreg Mar 17 at 21:07
1  
@FK82 First: you can't use a command such as \underline (well, you should never underline text, but that's by way of example). Second, a command such as \itshape should always be used in a group, so the end of the group would reestablish the previous conditions without an explicit font change that might be wrong. Third: try the command in a section title and you'll see. –  egreg Mar 17 at 21:20
1  
Another improvement, for more elegant code: Increment \mytemp immediately after \begingroup, and the \endgroup will restore it to the zero value automatically. –  alexis Mar 17 at 22:05
1  
But you still set it to false manually... Anyway, why don't you just use \newif\emphasis? Then you can say things like \ifemphasis and \emphasistrue. –  alexis Mar 17 at 22:21
show 11 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.