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.

What are the pros and cons of using active characters instead of macros in text? Would it make sense to implement a “simplified markup” parser in Latex to make code more readable?

Rationale

Latex-style inline markup can get distracting quickly. Most people, like me, probably only use a limited set of commands. (We’ll ignore environments for now.) One can define shorter mnemonic macros like \bold to alias \textbf, but that hardly saves any keystrokes – not for \, { and } in particular, which are difficult to access in some keyboard layouts.

At the same time, many (ASCII) punctuation marks are not used much or only in specific spatial scenarios, i.e. some are surrounded by whitespace characters, others have a space on one side and a letter on the other, yet others only follow or precede digits (maybe separated by a space) or are only useful in math mode altogether. French differs as usual.

Many people, like me, are used to markdown (as used e.g. on SX), reStructuredText (rST), textile, ASCIIdoc, Emacs org-mode, Haddock or some variant of wiki markup (e.g. MediaWiki or MoinMoin syntax). They all reuse punctuation characters to mark-up plain text without adding too much visible noise.

Some of these are already active characters in Latex, e.g. \, {, }, ', & and $, ^ and _ in math mode, with some babel.sty variants or enquote.sty options " and ~, too. One could activate many more to do away with verbose macros. I think that is how wiki.sty works. The tipa.sty magic may work in a similar way. (I prefer direct Unicode entry, though.) The thing is, everyone has slightly different needs so these shortcuts should be configurable.

I’m considering to try to write some code that makes it easy to add such shortcuts to one’s documents, maybe even up to the point where the source between \begin{document} and \end{document} becomes indiscernible from markdown or the like.

Alas, I really don’t know much about active characters and catcodes yet. If everybody thinks what I want to do is a Bad Idea, I will spare myself the hassle. If someone more knowledgeable would do (or actually has done) it instead, all the better.

Background

I’m writing linguistic texts about grapheme–phoneme interrelations, therefore I frequently need to include phonologic or graphematic notation, often in runs of normal prose. I defined macros like \grapheme{} and \phoneme{} for that, because graphemes are by convention enclosed in angular brackets \langle…\rangle which are cumbersome to include otherwise. The slashes of phonemic transcription would be easy to input on the other hand, but I also prefer a sans-serif font for that, so a command makes sense not only for structural reasons. There’s also \phone{} for phonetic notation of phones enclosed in square brackets, which are also used for properties (whose identifiers are uppercase or set in small capitals). Since I’m into grammatology, I sometimes even need to discriminate categoric letters and characters from actual graphs and glyphs, but let’s assume I surround them all by vertical bars with the help of \graphe{}. Mostly for completeness, I also define \morph{} and \morpheme{} in curly braces as well as \lexeme[]{} which just typesets the mandatory argument in italic and the optional one in single quotes.

\newcommand\wrong[1]{*#1}
\newcommand\ambig[1]{\textsuperscript{?}#1}
\newcommand\phone[1]{\mbox{[\textsf{#1}]}}
\newcommand\phoneme[1]{\mbox{/\textsf{#1}/}}
\newcommand\graphe[1]{\mbox{|#1|}} % SIC!
\newcommand\grapheme[1]{\mbox{$\langle$#1$\rangle$}}
\newcommand\morph[2][]{\mbox{\textup{\{}#2\textup{\}}\textsubscript{#1}}}
\newcommand\morpheme[1]{\morph{\textsc{#1}}}
\newcommand\property[2][±]{\mbox{\textup{[}$#1$#2\textup{]}}}
\newcommand\lexeme[2][]{\textit{#2}\ifthenelse{\isempty{#1}}{}{ ‘#1’}}}

For my needs, I would hence try to get these mappings (subject to change):

  • [x]\phone{x}
  • /x/\phoneme{x}
  • <x>\grapheme{x}
  • |x|\graphe{x}
  • \{x\} ? → \morph{x}
  • ((x)) ? → \morpheme{x}
  • [[x]], [y[x]], [[x]y]\property[y]{x}
  • >x<, >x<y>, <y>x<\lexeme[y]{x}
  • *x* or *x\wrong{x}
  • ?x? or ?x\ambig{x}

Some of the following mappings may also make sense for more generic use:

  • _x_ or ~x~\emph{x}
    • __x__ or ~~x~~\textit{x}
  • *x*\alert{x} or \strong{x}
    • **x** \textbf{x}
    • \NL*\item with automatic itemize environment if necessary, also # for enumerate and ; or : for description
  • +x+\textsc{x} / \person{x} or {\large x}
    • ++x++\MakeUppercase{x} or {\Large x}
  • =x=\chapter{x}
    • ==x==section{x} etc.
  • §x§\autocite{x}
  • @x@\ref{x}, \cref{x}
  • ^x^\footnote{x}

PS: I know I could use pandoc and similar tools to generate make .tex files, but they’re usually hard to adapt to ones individual Latex needs, too.
PPS: I also know I might encounter problems with beamer.cls, because of its ubiquitous use of < and >.

share|improve this question
    
See this answer tex.stackexchange.com/questions/154613/… for how to temporarily turn something active, with a switch to turn it back off again. –  Steven B. Segletes Apr 2 at 11:21
    
What if you want to use a question mark as itself? Or an equals sign in math? –  egreg Apr 2 at 11:56
    
@egreg, the idea is that <letter><question mark><space> is treated differently than <space><question mark><letter>. –  Crissov Apr 3 at 20:01
    
I just learned about \XeTeXinterchartoks etc.. I guess one could hack something up using that, too. –  Crissov May 13 at 15:05

1 Answer 1

up vote 2 down vote accepted

As I pointed out in my comment, if there is worry about breaking things by making characters active, you can set them up with toggles to turn them on and off. Here, I demonstrate with the left bracket and the macros \newphoneON and \newphoneOFF. Use the latter when you need to employ the left bracket in its normal configuration (e.g., as an optional macro argument).

UPDATED to show how to do it when the closing character is also active, as with the case of the slash and \newphonemeON and \newphonemeOFF with usage /abc/ (i.e., \newleftslash must be defined when the slash is active).

REVISED to semi-automate the process of producing these active character macros. I've put a number of them in, but the rest will take more thought, since they involve optional arguments, and worse still, symmetry of active characters... for example, I can set up to parse <x> by making < active; but then, if I want to parse >x< by making > active, it screws up the prior definition.

\documentclass{article}
\usepackage[T1]{fontenc}
\textwidth 2.5in\parskip 1em
\newcommand\wrong[1]{\raisebox{-3pt}{*}#1}% EDITED TO PROVIDE DIFFERENT LOOK
\newcommand\ambig[1]{\textsuperscript{?}#1}
\newcommand\phone[1]{\mbox{[\textsf{#1}]}}
\newcommand\phoneme[1]{\mbox{/\textsf{#1}/}}
\newcommand\graphe[1]{\mbox{|\textsf{#1}|}} % SIC!% EDITED TO PROVIDE DIFFERENT LOOK
\newcommand\grapheme[1]{\mbox{$\langle$#1$\rangle$}}
\newcommand\morph[2][]{\mbox{\textup{\{}#2\textup{\}}\textsubscript{#1}}}
\newcommand\morpheme[1]{\morph{\textsc{#1}}}
\newcommand\property[2][±]{\mbox{\textup{[}$#1$#2\textup{]}}}
\newcommand\lexeme[2][]{\textit{#2}\ifthenelse{\isempty{#1}}{}{ ‘#1’}}

\newcommand\newactive[3]{%
 \expandafter\let\csname sv#3\endcsname#1
 \expandafter\gdef\csname new#3\endcsname##1#2{\csname #3\endcsname{##1}}
 \expandafter\def\csname #3ON\endcsname{%
  \catcode`#1\active\def#1{\csname new#3\endcsname}}
 \expandafter\def\csname #3OFF\endcsname{%
  \catcode`#1=12\expandafter\let\expandafter#1\csname sv#3\endcsname}
}

\catcode`[\active\newactive{[}{]}{phone}\catcode`[=12
\catcode`/\active\newactive{/}{/}{phoneme}\catcode`/=12
\catcode`<\active\newactive{<}{>}{grapheme}\catcode`<=12
\catcode`|\active\newactive{|}{|}{graphe}\catcode`|=12
\catcode`*\active\newactive{*}{*}{wrong}\catcode`*=12
\catcode`?\active\newactive{?}{?}{ambig}\catcode`?=12

\def\linguisticsON{\phoneON\phonemeON\graphemeON\ambigON\wrongON\grapheON}
\def\linguisticsOFF{\phoneOFF\phonemeOFF\graphemeOFF\ambigOFF\wrongOFF\grapheOFF}
\begin{document}
Testing normal defs:  [[[xyz] /abc/ <x> ?x? *X* |xxx|\par\linguisticsON
Testing phone [xyz],   phoneme /abc/, grapheme <xyz>, ambig ?pdq?, wrong *X*, graphe |xyz|\par
\linguisticsOFF back to normal: [[[xyz]]]]]  /abc/ <xyz> ?xyz? *X*  |xxx|.
\end{document}

enter image description here


ORIGINAL SOLUTION

\documentclass{article}
\usepackage[T1]{fontenc}
\textwidth 2in\parskip 1ex
\newcommand\wrong[1]{*#1}
\newcommand\ambig[1]{\textsuperscript{?}#1}
\newcommand\phone[1]{\mbox{[\textsf{#1}]}}
\newcommand\phoneme[1]{\mbox{/\textsf{#1}/}}
\newcommand\graphe[1]{\mbox{|#1|}} % SIC!
\newcommand\grapheme[1]{\mbox{$\langle$#1$\rangle$}}
\newcommand\morph[2][]{\mbox{\textup{\{}#2\textup{\}}\textsubscript{#1}}}
\newcommand\morpheme[1]{\morph{\textsc{#1}}}
\newcommand\property[2][±]{\mbox{\textup{[}$#1$#2\textup{]}}}
\newcommand\lexeme[2][]{\textit{#2}\ifthenelse{\isempty{#1}}{}{ ‘#1’}}

\let\svleftbracket[
\def\newleftbracket#1]{\phone{#1}}
\catcode`[\active
\def\newphoneON{\catcode`[\active\def[{\newleftbracket}}
\def\newphoneOFF{\catcode`[=12\let[\svleftbracket}
\catcode`[=12

\let\svslash/
\catcode`/\active
\def\newleftslash#1/{\phoneme{#1}}
\def\newphonemeON{\catcode`/\active\def/{\newleftslash}}
\def\newphonemeOFF{\catcode`/=12\let/\svslash}
\catcode`/=12

\begin{document}
Testing a normal bracket and slash [[[xyz] /abc/\par
\newphoneON \newphonemeON
Testing the newphoneON [xyz] here and newphonemeON here /abc/ and /def/.\par
\newphoneOFF \newphonemeOFF back to normal bracket [[[xyz]]]]] and slash /abc/.
\end{document}
share|improve this answer
    
Can this be automated by a new command as in \newactive{/}{/}{phoneme}{leftslash} and \newactive{[}{]}{phone}{leftbracket}? –  Crissov Apr 2 at 14:36
    
@Crissov Please see revision. A step closer. –  Steven B. Segletes Apr 2 at 15:50

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.