TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to get LaTeX to render

${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$

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?

share|improve this question
You can define newcommands to do this, but you'll have to be careful and they'll probably need to take extra arguments. Perhaps it could be easier to define your own shortcut newcommands for commonly used functions, i.e. \newcommand{\cospar}[1]{\cos\left( #1\right} for cosine with built in parentheses. – qubyte Nov 21 '11 at 16:40
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:


  \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)%



% 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$


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:

  \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)%
share|improve this answer
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.

share|improve this answer
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

\def\log \cos x{\Log(\cos(x))}     

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

share|improve this answer
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$,


${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$

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$
share|improve this answer

Your Answer


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.