# Add missing () to mathematical functions

I am trying to get LaTeX to render

${f}^\prime = e^{x\,\log \cos x}\,\left(x\,\log \cos x\right)^\prime$


as

${f}^\prime = e^{x\,\log \left (\cos \left (x \right ) \right )}\,\left(x\,\log \left (\cos \left (x \right ) \right )\right)^\prime$


thus automatically adding the missing (). If the functions have more than one argument, the parentheses will already be added (so there's no ambiguity). Does anyone know a quick and simple way to accomplish this?

-
Sorry, that should have been \newcommand{\cospar}[1]{\cos\left(#1\right)}. I missed the last bracket. –  qubyte Nov 21 '11 at 16:56
Thanks Mark, that was exactly what I was looking for! –  Firona Nov 21 '11 at 17:12
No problem. I probably should have put it in as an answer! Happy TeXing! –  qubyte Nov 21 '11 at 17:49

Not completely, what you wanted, but almost:

\documentclass{article}

\newcommand*{\redefinesymbolwitharg}[1]{%
\expandafter\newcommand\csname ltx#1\endcsname{}%
\expandafter\let\csname ltx#1\expandafter\endcsname\csname #1\endcsname
\expandafter\renewcommand\csname #1\endcsname[1]{%
\csname ltx#1\endcsname\left(##1\right)%
}%
}

\redefinesymbolwitharg{cos}
\redefinesymbolwitharg{log}

\begin{document}

% You may use \ltxcos, \ltxlog etc. if you need the original definition
% without argument:
${f}^\prime = e^{x \ltxlog \left (\ltxcos \left (x \right ) \right )}\,\left(x \ltxlog \left (\ltxcos \left (x \right ) \right )\right)^\prime$

% But \cos and \log expect an argument now:
${f}^\prime = e^{x \log{\cos x}}\,\left(x \log{\cos x}\right)^\prime$

\end{document}


Here for the argument of \cos I've used the standard behavior of TeX, that if you don't use argument braces {} it uses the first token after the command to be the argument. This would work with each single characters coded with a single byte (e.g. \cos ß would fail if you're using \usepackage[utf8]{inputenc} but \cos{ß} or \cos \beta would be correct).

And if you prefer to write \redefinesymbolwitharg\cos instead of \redefinesymbolswitharg{cos} you may replace definition of \redefinesymbolwitharg by:

\makeatletter
\newcommand*{\redefinesymbolwitharg}[1]{%
\edef\@tempa{\expandafter\@gobble\string #1}%
\expandafter\let\csname ltx\@tempa\expandafter\endcsname\csname \@tempa\endcsname
\expandafter\renewcommand\csname \@tempa\endcsname[1]{%
\csname ltx\@tempa\endcsname\left(##1\right)%
}%
}
\makeatother

-
This has problem if there is more than one character: \cos xy does not produce cos(xy). –  Peter Grill Nov 21 '11 at 17:31
@Peter Gill: I've never told something diffent. You have to use \cos{xy} in this case. Compare it with usage of \log I've shown. That's the reason I've told: Not completely, what you wanted. –  Schweinebacke Nov 21 '11 at 17:35
I did not mean to imply you said anything different, just thought that should be pointed out. I did this solution at first as well, and only then afterwards realized this problem. Also, excellent idea to generalize the solution via \redefinesymbolwitharg -- that did not occur to me. –  Peter Grill Nov 21 '11 at 17:44
@Peter Grill: In this case, we should tell the user, that omitting the braces may only be done for single characters, that are coded with a single byte and e.g. \cos ß instead of \cos{ß} would fail using \usepackage[utf8]{inputenc}. But is "How TeX finds macro arguments?" the topic? –  Schweinebacke Nov 21 '11 at 17:49

Mark's suggestion is a good one. But if you don't want to deal with \newcommand you can add the "()" without \left of \right as follows:

 $f^\prime = e^{x\,\log (\cos (x))}\,(x\,\log (\cos (x)))^\prime$


The result is almost exactly what you want except for a little difference in the space preceding the brackets.

-
That's not quite accurate... Nested brackets are automatically resized when preceded by \left and \right, while this is not happening otherwise. Plus these two commands let LaTeX 'know' that these go in pairs. That is why you have for instance \right. that does not produce any visible bracket. –  Count Zero Nov 21 '11 at 17:05
Thanks Leonardo, but with your solution I would still need to add the brackets manually. –  Firona Nov 21 '11 at 17:13
@CountZero You're right, but in this particular case the presence of '\left \right' is not so relevant. The output looks very similar with or without these commands. –  Leonardo Nov 21 '11 at 17:14

The following works if you always write \log \cos x

\documentclass{article}
\let\Log\log
\def\log \cos x{\Log(\cos(x))}
\begin{document}

${f}^\prime = e^{x\,\log \cos x}\,(x\,\log \cos x)^\prime$

\end{document}

-
This also has problem if there is more than one character: \cos xy does not produce cos(xy). –  Peter Grill Nov 21 '11 at 17:31
@Peter Gill: That's the reason Herbert wrote: if you always write \log \cos x, isn't it? –  Schweinebacke Nov 21 '11 at 17:38
This is one of the advantages of using plain TeX \def for macro definitions. –  Werner Nov 21 '11 at 17:45
@Peter Grill: In my future answers I'll have a special paragraph in bold with informations only for you ... –  Herbert Nov 21 '11 at 18:19
:-) Thanks Herbert. Any help in make this site more useful for me is great. :-) :-) –  Peter Grill Nov 21 '11 at 18:31

I think Mark's suggestion is the way to go. But, you do need to be aware that there are problems unless you enclose the args of the \cos within a {} when there is more than one character involved as in the last case here.

This is no different than other functions if you leave out the {} as in $\frac 12a$,

\documentclass{article}

\newcommand{\Cos}[1]{\cos\left(#1\right)}%
\begin{document}
${f}^\prime = e^{x\,\log \cos x}\,\left(x\,\log \cos x\right)^\prime$

${f}^\prime = e^{x\,\log \left (\cos \left (x \right ) \right )}\,\left(x\,\log \left (\cos \left (x \right ) \right )\right)^\prime$

\bigskip
Now using \textbackslash{}Cos:\par
${f}^\prime = e^{x\,\log \Cos x}\,\left(x\,\log \Cos x\right)^\prime$

${f}^\prime = e^{x\,\log \Cos {x\pi}}\,\left(x\,\log \Cos {x\pi}\right)^\prime$
\end{document}

-