New answer: You want to do an "input substitution", as compared with an "output substitution". That is, you want to replace [
with [.
as material in the .tex file, rather than as printable material. More precisely, you don't want to have the braces replace themselves "as you go along", because the \Tree
command will absorb them all at once and not allow them to be executed before it parses what's written, which will not have the dots yet inserted.
In short, what you want is to do a string substitution replacing [
with [.
, saving the result in a macro that can then be expanded after \Tree
to give it the correct syntax. For this, the stringstrings
package seems ideal. Here's a minimal document showing how you can define a macro to automate the whole process for you:
\documentclass{article}
\usepackage{stringstrings,qtree,linguex}
\newcommand*\dotify[1]{%
\convertchar[q]{#1}{[}{[.}%
}
\newcommand*\doboth[1]{%
\dotify{#1}%
\ex. \expandafter\Tree\thestring
\exi. #1
}
\begin{document}
\doboth{[S [NP This ] [VP [V is ] [NP an example ]]]}
\end{document}
Old answer: If you do want to do something like this with catcode methods, you still have a few problems. Most critically, you can't make catcode changes to a macro argument that has already been read, so you should not declare \dotify
as having an argument. Instead, you should use the braces delimiting its "argument" to contain a group that "sets up" the correct parsing context; see this interesting answer for the source (for me) of this trick. The group will naturally restrict the scope of the catcode change as well; a bonus.
Your other problem is that \[
means \begin{equation}
and not a left brace. I'm sure there's already a command for that, but you can define your own with \let\lb[
.
With these changes, the correct code would be:
\documentclass{article}
\def\activebrace{\catcode`\[=\active}
\let\lb[
{\activebrace \global\def[{\lb.}}
\def\dotify#{%
\bgroup
\activebrace
\let\next=%
}
\begin{document}
\dotify{[a[b[c]]]}
\end{document}
which outputs [.a[.b[.c]]]
.