Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Whats the diff between \texttt and \tt?

5,216 views
Skip to first unread message

tan woon kiong

unread,
Dec 16, 2001, 9:56:35 PM12/16/01
to
Hi,

I would like to understand the difference between \texttt and \tt?
Why is it that chunk-A and chunk-B typeset differently?

%%%%%%%%%%<chunk-A>%%%%%%%%%%
$$
\texttt{@A\_b}_1 = \sum_x ...
$$
%%%%%%%%%%</chunk-A>%%%%%%%%%%

%%%%%%%%%%<chunk-B>%%%%%%%%%%
$$
{\tt @A\_b}_1 = \sum_x ...
\end{align*}
$$
%%%%%%%%%%</chunk-B>%%%%%%%%%%

Regards,
- WK

Olaf Dietrich

unread,
Dec 17, 2001, 4:58:51 AM12/17/01
to
tan woon kiong <tts...@pacific.net.sg>:

>
> I would like to understand the difference between \texttt and \tt?

\tt is obsolete in LaTeX (not plainTeX). You should
use only \texttt or \mathtt - and the latter gives
you the same "@" as the obsolete \tt.

> $$
> \texttt{@A\_b}_1 = \sum_x ...
> $$

"$$" is plainTeX not LaTeX; use \[...\] instead
(or the equation or displaymath environments).
For example, the class option "fleqn" does not
work with "$$".

Olaf
--
Before posting to comp.text.tex, please consult:
- LaTeX Introduction <URL:http://www.ctan.org/tex-archive/info/lshort/english/>
- Symbol List <URL:http://www.ctan.org/tex-archive/info/symbols/comprehensive/>
- UK TeX FAQ <URL:http://www.tex.ac.uk/faq>

Donald Arseneau

unread,
Dec 17, 2001, 5:36:05 AM12/17/01
to
tan woon kiong <tts...@pacific.net.sg> writes:

> I would like to understand the difference between \texttt and \tt?

\tt is a switch while \texttt takes an argument.
\tt appies to either text or math, \texttt (\mathtt) applies to
text (math).

Donald Arseneau as...@triumf.ca

tan woon kiong

unread,
Dec 17, 2001, 10:44:13 AM12/17/01
to
Olaf Dietrich <o...@dtrx.de> writes:

> tan woon kiong <tts...@pacific.net.sg>:
> >
> > I would like to understand the difference between \texttt and \tt?
>
> \tt is obsolete in LaTeX (not plainTeX). You should
> use only \texttt or \mathtt - and the latter gives
> you the same "@" as the obsolete \tt.
>
> > $$
> > \texttt{@A\_b}_1 = \sum_x ...
> > $$
>
> "$$" is plainTeX not LaTeX; use \[...\] instead
> (or the equation or displaymath environments).
> For example, the class option "fleqn" does not
> work with "$$".
>

The rationale for this question is due to noweb. I use noweb as a
literate programming tool. The usage of noweb is similar to cweb. We
write a literate source, say hello.nw. The noweb is used to generate
the document hello.tex from hello.nw.

noweave hello.nw ==> hello.tex

In literate source, variable names (of programming language) that we
want to typeset in tt are place in the [[...]] construct. This
construct [[...]] can be viewed as some kind of shortcut for
typesetting stuff that needs to be in verbatim and in the tt font. The
way [[...]] works is by the following translation.

[[...]] ==> {\tt{}...}

For example (I have simplied the following a bit):
[[@A_b]] ==> {\Tt{}@A\_b}
where
\Tt=\tt

The [[...]] construct works perfectly in non-maths mode, but it
typesets wrongly in maths mode. I do not want to modify the
translation scheme of [[...]]. I would like to modify the definition
of \Tt instead. How should I redefine \Tt such that

{\Tt{}@A\_b} works like \texttt{@A\_b}.


Thanks,
- WK

Dan Luecking

unread,
Dec 17, 2001, 5:15:51 PM12/17/01
to
Donald Arseneau <as...@triumf.ca> wrote in message news:<yfi8zc2...@triumf.ca>...

> tan woon kiong <tts...@pacific.net.sg> writes:
>
> > I would like to understand the difference between \texttt and \tt?
>
> \tt is a switch while \texttt takes an argument.
> \tt appies to either text or math, \texttt (\mathtt) applies to
> text (math).

The main difference is that \texttt is for text and changes fonts
the way font changes are carried out in text. Normally in math,
however font changes are carried out more subtly.

Greatly over simplifying: math has families rather than fonts. A
family is (usually) a set of three fonts (for regular, subscripts
and subsubscripts. Most symbols are defined to select a particular
character from a particular family. The exception are letters, which
are said to have variable family. A math command like \mathtt (or a
switch like \tt) simply changes the family, and only letters are
affected. That's why you get a cmr10 "@" with cmtt letters: @ is
not a letter.

Dan Luecking

Giuseppe Bilotta

unread,
Dec 18, 2001, 4:35:25 AM12/18/01
to
tan woon kiong wrote:
> How should I redefine \Tt such that
>
> {\Tt{}@A\_b} works like \texttt{@A\_b}.
>

\let\Tt\ttfamily

--
Giuseppe "Oblomov" Bilotta

Axiom I of the Giuseppe Bilotta
theory of IT:
Anything is better than MS

Robin Fairbairns

unread,
Dec 18, 2001, 7:03:04 AM12/18/01
to
Giuseppe Bilotta <obl...@freemail.it> wrote:
>tan woon kiong wrote:
>> How should I redefine \Tt such that
>>
>> {\Tt{}@A\_b} works like \texttt{@A\_b}.
>>
>
>\let\Tt\ttfamily

*\let\Tt\ttfamily
*${\Tt{}@A\_b}$

! LaTeX Error: Command \ttfamily invalid in math mode.

how about

\def\Tt{%
\ifmmode\def\next{%
\afterassignment\RelayTt
\toks0\bgroup
}%
\else
\let\next\ttfamily
\fi
\next
}
\def\RelayTt{\expandafter\texttt\expandafter{\the\toks0}\egroup}

(whew)
--
Robin Fairbairns, Cambridge -- rf10 at cam dot ac dot uk

David Kastrup

unread,
Dec 18, 2001, 7:08:32 AM12/18/01
to
r...@pallas.cl.cam.ac.uk (Robin Fairbairns) writes:

> Giuseppe Bilotta <obl...@freemail.it> wrote:
> >tan woon kiong wrote:
> >> How should I redefine \Tt such that
> >>
> >> {\Tt{}@A\_b} works like \texttt{@A\_b}.
> >>
> >
> >\let\Tt\ttfamily
>
> *\let\Tt\ttfamily
> *${\Tt{}@A\_b}$
>
> ! LaTeX Error: Command \ttfamily invalid in math mode.
>
> how about
>
> \def\Tt{%
> \ifmmode\def\next{%
> \afterassignment\RelayTt
> \toks0\bgroup
> }%
> \else
> \let\next\ttfamily
> \fi
> \next
> }
> \def\RelayTt{\expandafter\texttt\expandafter{\the\toks0}\egroup}

Put \relax before \ifmmode, or \Tt might fail as the first element of
an alignment entry.

--
David Kastrup, Kriemhildstr. 15, 44793 Bochum
Email: David....@t-online.de

tan woon kiong

unread,
Dec 18, 2001, 10:21:41 AM12/18/01
to
David Kastrup <David....@t-online.de> writes:

> r...@pallas.cl.cam.ac.uk (Robin Fairbairns) writes:
>
> > Giuseppe Bilotta <obl...@freemail.it> wrote:
> > >tan woon kiong wrote:
> > >> How should I redefine \Tt such that
> > >>
> > >> {\Tt{}@A\_b} works like \texttt{@A\_b}.
> > >>
> > >
> > >\let\Tt\ttfamily
> >
> > *\let\Tt\ttfamily
> > *${\Tt{}@A\_b}$
> >
> > ! LaTeX Error: Command \ttfamily invalid in math mode.
> >
> > how about
> >
> > \def\Tt{%
> > \ifmmode\def\next{%
> > \afterassignment\RelayTt
> > \toks0\bgroup
> > }%
> > \else
> > \let\next\ttfamily
> > \fi
> > \next
> > }
> > \def\RelayTt{\expandafter\texttt\expandafter{\the\toks0}\egroup}
>
> Put \relax before \ifmmode, or \Tt might fail as the first element of
> an alignment entry.
>

> [...]

Thanks a lot :-) The definition of \Tt works perfectly for maths and
non-maths mode.

Please pardon me for pushing the question a little further. I tried to
use \Tt in the section command as follows:

\section{${\Tt{}@A\_b}$}

However, TeX refuses.

%---------- TeX Error Message ----------
ERROR: Undefined control sequence.

--- TeX said ---
\Tt ... \RelayTt \toks 0\bgroup }\else \let \next
\ttfamily \fi \next
l.35 \section{${\Tt{}@A\_b}$}
%------------------ End ------------------


When I modify \Tt to use \c in place of \next as follows, \Tt works in
\section as well.

%---------- Modified \Tt ----------
\def\Tt{%
\relax
\ifmmode\def\c{%


\afterassignment\RelayTt
\toks0\bgroup
}%
\else

\let\c\ttfamily
\fi
\c


}
\def\RelayTt{\expandafter\texttt\expandafter{\the\toks0}\egroup}

%------------------ End ------------------


I do not understand why it is that changing \next to \c in the
definition of \Tt seems to fix the problem (or does it)? Why does the
name of this temporary macro matter?

Thanks,
- WK

David Kastrup

unread,
Dec 18, 2001, 10:54:40 AM12/18/01
to
tan woon kiong <tts...@pacific.net.sg> writes:

> Please pardon me for pushing the question a little further. I tried to
> use \Tt in the section command as follows:
>
> \section{${\Tt{}@A\_b}$}
>
> However, TeX refuses.

That's a moving argument. Define \Tt with \DeclareRobustCommand
instead of \newcommand if you want it to work there as well.

Robin Fairbairns

unread,
Dec 18, 2001, 12:19:33 PM12/18/01
to
tan woon kiong <tts...@pacific.net.sg> wrote:
>David Kastrup <David....@t-online.de> writes:
>> r...@pallas.cl.cam.ac.uk (Robin Fairbairns) writes:
>> > \def\Tt{%
>> > \ifmmode\def\next{%
>> > \afterassignment\RelayTt
>> > \toks0\bgroup
>> > }%
>> > \else
>> > \let\next\ttfamily
>> > \fi
>> > \next
>> > }
>> > \def\RelayTt{\expandafter\texttt\expandafter{\the\toks0}\egroup}
>>
>> Put \relax before \ifmmode, or \Tt might fail as the first element of
>> an alignment entry.
>>
>> [...]
>
>Thanks a lot :-) The definition of \Tt works perfectly for maths and
>non-maths mode.
>
>Please pardon me for pushing the question a little further. I tried to
>use \Tt in the section command as follows:
>
> \section{${\Tt{}@A\_b}$}
>
>However, TeX refuses.

because it's a fragile command in a moving argument.

\protect\Tt will solve that, i would assume.

>When I modify \Tt to use \c in place of \next as follows, \Tt works in
>\section as well.

>[...]


>I do not understand why it is that changing \next to \c in the
>definition of \Tt seems to fix the problem (or does it)? Why does the
>name of this temporary macro matter?

possibly because \c is already defined as a robust command? no, that
can't be right. whatever, just don't do it: if the command is going
to be used so often that \protect would become tiresome,

\DeclareRobustCommand{\Tt}{...}

instead of \def\Tt

Giuseppe Bilotta

unread,
Dec 18, 2001, 5:10:52 PM12/18/01
to
Robin Fairbairns wrote:
> Giuseppe Bilotta <obl...@freemail.it> wrote:
> >tan woon kiong wrote:
> >> How should I redefine \Tt such that
> >>
> >> {\Tt{}@A\_b} works like \texttt{@A\_b}.
> >>
> >
> >\let\Tt\ttfamily
>
> *\let\Tt\ttfamily
> *${\Tt{}@A\_b}$
>
> ! LaTeX Error: Command \ttfamily invalid in math mode.

Well stupid me I should have read the original post better


--
Giuseppe Bilotta

"I'm never quite so stupid
as when I'm being smart" --Linus van Pelt

tan woon kiong

unread,
Dec 19, 2001, 12:41:48 PM12/19/01
to
r...@pallas.cl.cam.ac.uk (Robin Fairbairns) writes:

> tan woon kiong <tts...@pacific.net.sg> wrote:
> >David Kastrup <David....@t-online.de> writes:
> >> r...@pallas.cl.cam.ac.uk (Robin Fairbairns) writes:
> >> > \def\Tt{%

> >> > [...]


> >> > }
> >> > \def\RelayTt{\expandafter\texttt\expandafter{\the\toks0}\egroup}
> >>
> >> Put \relax before \ifmmode, or \Tt might fail as the first element of
> >> an alignment entry.
> >>
> >> [...]
> >

> >Please pardon me for pushing the question a little further. I tried to
> >use \Tt in the section command as follows:
> >
> > \section{${\Tt{}@A\_b}$}
> >
> >However, TeX refuses.
>
> because it's a fragile command in a moving argument.
>
> \protect\Tt will solve that, i would assume.
>

[...]


>
> \DeclareRobustCommand{\Tt}{...}
>
> instead of \def\Tt
> --
> Robin Fairbairns, Cambridge -- rf10 at cam dot ac dot uk

Thank you all very much! The macro \Tt is perfect now. I have
injected the definition of \Tt into my noweb installation. I will be
using it for all my noweb codes from now on.

Thanks,
- WK

0 new messages