# Apply formatting to outermost parentheses

I have some text information where I need to apply a text format to all the outer most parentheses such as making them larger and/or bold.

In a sense I want to process text like

^(...(...(...)...)...(..)..^)...^(..(....)..^)

Where ^( and ^) have special formats(basically all I want to do is make the outermost parentheses of some mathematical like text(but it is not math text) stand out so it is easier to read).

It would look like

(...(...(...)...)...(..)..)...(..(....)..)

Any ideas to do this easily? I do not want to have to add \mathbf to each parenthesis as it clutters up the text.

(in fact I would actually like to modify the text just before *( and apply a format to it(just the word before it).

So

SomeText(....)

would get a format applied to SomeText and the outer ( ) but everything inside will remain.

I want to avoid cluttering up the text as much as possible and I guess some environment is needed?

-
for just the parentheses you can make them active, do some counting and (as long as your parentheses are well balanced (possibly within an environment)) that should be relatively easy. I don't see an immediate way to extend that to the word right in front of the parentheses though. –  Roelof Spijker Dec 6 '11 at 8:23

I wouldn't mess with catcodes, as you never know the implications to other packages, especially for math, but rather provide a small parser. The author inserts the input as:

 $\parser SomeWords (...(\alpha...(...)...)...(\beta)..)...(..(....)..);$


You can give the \parser a more semantic meaning if you like, anything between it and the first opening bracket is then typeset in bold. If the string does not contain math you can omit the $..$.

We parse the content between the first opening parenthesis and the last one up to the semicolon, letter by letter using the LaTeX kernel loop @tfor. We keep a balance between the outer and inner loops and typeset accordingly. Here is the result:

The MWE is shown below:

\documentclass{article}
\begin{document}
\makeatletter
\def\L{(}
\def\R{)}
%left counter
\newcounter{cnt}
\setcounter{cnt}{1}
%right counter
\newcounter{cntr}
\setcounter{cntr}{1}
%new counter balancing
\newcounter{bal}
\setcounter{bal}{0}
%define the parser
\def\parser#1(#2);{%
\textbf{#1 (}
\@tfor\next:=#2\do{%
\ifx\next\L \stepcounter{cnt}
\stepcounter{bal}
\ifnum\thebal=0 \textbf{\next}\else\normalfont\next\fi%
%
\else
\ifx\next\R \stepcounter{cntr}
\ifnum\thebal=-1 \textbf{\next}\else\next\fi%
\else
\next
\fi
\fi
}%end forloop
\textbf{)}
}

$\parser SomeWords (...(\alpha...(...)...)...(\beta)..)...(..(....)..);$

\end{document}

-

Maybe \lgroup/\rgroup for the outer parentheses?

$$\lgroup a(b(c)) (d)\rgroup \lgroup e(fg) h\rgroup$$
\bye


For the macro, you could use the parentheses as macro delimiters, and a space as the end delimiter (in the example, the line change counts as a space, that is, an end delimiter):

\def\someFormat#1{{\it #1\/}}
\def\thingamabob#1(#2) {{\someFormat{#1}\mathsurround0pt$\lgroup$#2$\rgroup$}}
\thingamabob SomeText(blah(foo)bar) \thingamabob (bar(baz)foo(blah))
\bye


The \mathsurround0pt is there only to make sure that there isn't any space added before/after mathmode changes in case it is set to >0, and the surrounding group is there to contain that setting inside this command.

-
Um, no, as I said, I want to get away from using all that stuff and have it done automatically for me(through some environment or something). –  AbstractDissonance Dec 6 '11 at 9:04
@AbstractDissonance: think of it as a partial answer (showing the glyphs that I thought you were after for). –  morbusg Dec 6 '11 at 10:34

The following provides \important[<prefix>]{<stuff>} that typesets prefix (in boldface, text mode) and surrounds <stuff> with \big( and \big) as a form of accentuation. Alternatively, an environment-style approach is also possible, provided by grp (short for "group"):

\documentclass{article}
\newcommand{\important}[2][]{\textbf{#1}\big(#2\big)}%
\newenvironment{grp}{\big(}{\big)}
\begin{document}
$\important[SomeText]{\ldots(\ldots(\ldots)\ldots)\ldots(\ldots)\ldots}\ldots\important{\ldots(\ldots)\ldots}$

$\textbf{SomeText}\begin{grp}\ldots(\ldots(\ldots)\ldots)\ldots(\ldots)\ldots\end{grp}\ldots\begin{grp}\ldots(\ldots)\ldots\end{grp}$
\end{document}

-
Well, I want to avoid all the "mark up". I want to be able to simply type: SomeText(...(...(...)...) instead of what you have typed. The "grammar" I am using is consistent throughout and I want to avoid as much "formatting" as possible as it adds bloat and confusion to the text. –  AbstractDissonance Dec 6 '11 at 9:04
Obviously to do it the way I want I'll either have to use a command like you have done(although I wouldn't use such a long name) or use an environment. I'd rather use an environment if it would be easy to create one. –  AbstractDissonance Dec 6 '11 at 9:06
Creating an environment to do the same is easy. I've updated my answer. It would be difficult to get away from an automated approach that only works on the outer parentheses. Using "markup" via some macro or environment is the easiest way. Moreover, there is some symbolic usage that is visible, which should improve code readability. –  Werner Dec 6 '11 at 15:22

Following @wh1t3's suggestion above, here is one way of defining an \outermost command that emphasises any text before the first opening parenthesis and then every pair of outermost parentheses thereafter. It needs to be isolated in a block if you don't want parentheses to be treated specially for the rest of the code.

The emphasis code can be changed to whatever you want: in particular I've assumed here that this will be used in text mode, so this uses \textbf, but \mathbf (or perhaps a poor man's bold command to deal with the parentheses) would work if you want to use this in math mode.

The main virtue of this approach is that no extra markup is needed to the parenthesis-containing text.

\documentclass{standalone} % for demonstration purposes

\makeatletter

\newcount\@nbegin % number of inner open parentheses in current block
\newcount\@nend   % number of outer open parentheses in current block

\newcommand\@outermostopen{%
\@nbegin=0\@nend=0\relax%
\@beginisinner\@endisouter%
\@outermostem{(}}
\newcommand\@outermostclose{%
\@outermostem{)}%
\@beginisouter}
\newcommand\@inneropen{%
(%
\@endisinner}
\newcommand\@innerclose{%
)%
\ifnum\@nend=\@nbegin%
\@endisouter\fi%
}

\def\outermost#1({%
\catcode(=\active
\catcode)=\active
\@outermostem{#1}\@outermostopen%
}

{ % Commands that redefine parentheses themselves need to be defined
% in a context where () are active
\catcode(=\active
\catcode)=\active
\gdef\@beginisouter{%
\def({\@outermostopen}%
}
\gdef\@beginisinner{%
\def({\@inneropen}%
}
\gdef\@endisouter{%
\def){\@outermostclose}%
}
\gdef\@endisinner{%
\def){\@innerclose}%
}
}

\newcommand{\@outermostem}[1]{\textbf{\Large #1}}
% redefine for whatever emphasis method you want
\makeatother

\begin{document}
{\outermost Beginning(\dots(\dots)\dots)\dots(\dots)\dots(\dots(\dots(\dots)\dots(\dots)\dots)\dots)}
\end{document}


which gives

-
An addendum, since I've just learnt a useful trick: you can define \def\Outermost#{\bgroup\afterassignment\outermost\let\@next= }, and then the string containing parentheses behaves like an ordinary argument: \Outermost{First (ab(cd)e(fg(h)i)jk)l(m(n)op)}. –  Ant Dec 11 '11 at 13:35