Showing posts with label commands. Show all posts
Showing posts with label commands. Show all posts

Thursday, July 23, 2009

Typesetting tilde or backslash

Typesetting a tilde in Latex or Xetex is not so easy. It's a reserved character (non breakable fixed width space) and \~ produces a diacritic. Typing this diacritic alone, i.e. \~{} still produces a tilde which is small and high above the base line. When you follow most of the recommendations in Latex books, you probably write tilde by $\sim$ or any other more complicated variant thereof to work in both text and math environment. This produces a tilde nice and big, not too high above the baseline. What you also see is the recommendation to write tilde in the verbatim mode. For me these two are still not acceptable because they usually switch the font and you really see that the font of it looks alien to the surrounding text. Why should I switch the font when I want to write a tilde in the text mode? I want to get a nice tilde in the normal font which is not a diacritic. Font designers strived to match the appearance of the tilde glyph to the rest of the font so why shouldn't I use it?

Fortunately, there is a macro for typing the kind of tilde I need: \textasciitilde{}. It is just not generally known. This produces a tilde in the text mode which uses the tilde glyph from the font just as the font designers shaped it. It's similar to the command \textbackslash{} which is more widely known for typsetting backslash in text mode.

When you are in text mode, use these commands and spread the word.

Wednesday, May 20, 2009

Phantomas command

Recently I have found that there is a useful TeX command \phantom{Phantom Text} which behaves just like it would typeset its argument except that nothing is typeset and there is a blank space of the corresponding dimensions.

When I have been using it I imagined it would be sometimes even more useful to have a command which can put some other stuff in the reserved space instead of the phantom stuff.

Luckily, some smart guys have already written such a command.

Here is a modified version of their minimal example which illustrates nicely the use of this command:


\documentclass{article}
\usepackage{calc} % for the \phantomas command

% The following command is a better version of \phantom and requires the "calc" package
% Credits: Jean-Côme Charpentier & Scott Pakin
% Source: comp.text.tex, "Phantom-ish command"
% usage: \phantomas[l]{phantom words which will be overwritten}{with these words}
% the optional parameter [l] says that the words "with these words" will appear aligned left to the reserved space
% another optional parameter is [r] for aligning the words right
% if no optional parameter is given, the words will be centred in the reserved space
\newcommand*\phantomas[3][c]{%
\ifmmode
\makebox[\widthof{$#2$}][#1]{$#3$}%
\else
\makebox[\widthof{#2}][#1]{#3}%
\fi
}

\begin{document}

A long entry another long entry and our last long entry\par
\phantomas{A long entry}{centred} \phantomas[l]{another long entry}{on the left}
\phantomas[r]{and our last long entry}{on the right}

$\sin^2 x + \cos^2 x = 1$\par
$\phantomas{\sin^2 x + \cos^2 x}{f(x)} = 1$\par
$\phantomas[l]{\sin^2 x + \cos^2 x}{f(x)} = 1$\par
$\phantomas[r]{\sin^2 x + \cos^2 x}{f(x)} = 1$

\end{document}

Wednesday, March 5, 2008

Assign another catcode

The actual and more elegant solution for my problem with verbatim text in argument position decribed in the previous post is much simpler. Since the underscore “_” is the only nasty character which does not allow me to write things like “NA071112-01_E.008” as an argument of my \ru command the best solution for me is to assign the underscore another catcode (thanks to Enrico Gregorio). This simply achieved by …


\catcode`\_=12


… in the preamble. Thereby the underscore looses its subscript function (category code 8) and is treated like any other puctuation character (category code 12).

As a linguist I do not need the underscore as a shortcut for subscript in math mode. If I want, I can still do it by “\sb” instead of the “_”. (\sb is actually short enough.)

I then can define my \ru just like this:


\providecommand{\ru}[1]{#1}


(I may want to fill it with additional commands later.)

I get the feeling that changing the category code of certain special characters may be the most accessible (if not the only) way of accepting verbatim text as an argument of a command.

This whole thing around my \ru command motivates me to go through some introduction to TeX. Very nice explanation of catcodes and their purpose I found in Eijkhout’s TeX by Topic on p. 29.

D. E. Knuth certainly was a smart guy.

Verbatim text as command argument

In my dissertation I will refer to the N|uu text corpus. The corpus has reference units which are called e.g. NA071112-01_E.008. I thought all the references to the text corpus are markup-worthy entities so I needed a command like \ru (short for reference unit) to markup those references with it like this: \ru{NA071112-01_E.008}.

As you know, the underscore “_” is a special character used in math mode for subscript. So it is not allowed to occur as a part of some string. I did not want to write \ru{NA071112-01\_E.008} instead. I looked for a way to feed commands verbatim text arguments.

I stumbled upon the fancyvrb package, which looks really promising for this, especially in the documentation on p. 17 where some magic aftersave is used. But the aftersave parameter is heavily underdocumented there. That’s just my luck. I could not find out what aftersave actually does and if it will enable me to write someting like \ru{NA071112-01_E.008}.

I then sought help at comp.text.tex (topic: fancyvrb problems) and Enrico Gregorio was so kind to write me this TeX code:


\def\ruspeciallist{\do\_} % add the special characters you need with "\do\X" (X is the characater)

\def\rucatcodes{\def\do##1{\catcode`##1=12 }\ruspeciallist}
\newbox\rubox
\def\ru{\afterassignment\dorusetup\let\next= }
\def\dorusetup{\setbox\rubox=\hbox\bgroup
\rucatcodes \aftergroup\dorufinish}
\def\dorufinish{\box\rubox}


An extensively commented version is also there.

In the last line between “\dorufinish{” and “\box\rubox}” come all the commands you want to apply the the argument of \ru. BUT:

Enrico: “A limit of this approach is that the string is never read as an
argument, so that it is not available for, say, writing an index
entry: we have it only in typeset form, inside the box.”

Ok, this certainly is a nice piece of code but it does not essentially allow verbatim text to be an argument of a command. It is just a work-around to get that verbatim text typeset somehow (and that's what I have originally asked for in the newsgroup). So, in that sense, verbatim text as argument still is an open issue.

Enrico — after I told him I actually may want to index that stuff — suggested a much simpler and elegant solution for my particular needs.

Anyway, if anybody of you readers finds out how to use the aftersave of fancyvrb package do inform me in the comments to this post. I’d really like to know. I think it has something to do with TeX’s \expandafter.

Newcommand with an optional argument

I don’t know why this feature is not implemented in LaTeX. You can very easily define your own commands, but they may take only a fixed number of arguments (up to 9). Bad luck, because the first command I needed to write for my dissertation had to have an optional argument:

Linguists traditionally write words and sounds from a language of interest in italics, e.g. the N|uu word ainki. Often it is followed by an enquoted translation, e.g. ainki ‘father’, xainki ‘mother’, n|ai ‘see’, and kx’ain ‘laugh’ all contain the diphthong ai.

So I wanted to write me a command \nuu by which I could write either just the N|uu word in italics or the word in italics followed by the enquoted translation. I would use it either just as \nuu{ainki} for ainki or as \nuu[father]{ainki} for ainki ‘father’.

Later, when I learn how to make an index of all N|uu words written in my document, I probably will just add an indexing command into the \nuu command definition. For now the optional argument was trouble enough. I did not expect that it will involve hardcore TeX programming.

Fortunately good people from the xetex@tug.org mailing list have suggested some solutions for this and the most bullet-proof and versatile was this (thanks to Morten Høgholm and Ross Moore):


\documentclass{article}

\makeatletter
\long\def\tlist@if@empty@nTF #1{%
\expandafter\ifx\expandafter\\\detokenize{#1}\\%
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}

\providecommand*\nuu[2][]{%
\textit{#2}%
\tlist@if@empty@nTF{#1}{}{ `#1'}% only the false code executed
}
\makeatother

\begin{document}
\nuu{ainki} \nuu[mother]{xainki} \nuu[goat]{mudi}
\end{document}


I use \providecommand instead of \newcommand. I feel safer. “It works like \newcommand, but if the command is already defined, LaTeX2e will silently ignore it.” (Oetiker et al., lshort.pdf, p. 109.)