2

I have a number of trig functions raised to some powers, and the arguments are often different from just t

\sin^2{\frac{2\pi}{T}t}

for example

Now, I would like to get the argument to be in parentheses, which I know I can input manually with

\sin^2{\left(\frac{2\pi}{T}t\right)}

but I would like to get it automatically.

Now, I've tried to redefine the symbol with argument, as shown here https://tex.stackexchange.com/a/35646/106445

but when I try to add the power I get two errors: Missing { inserted, and Missing } inserted. I assume it's because it's expecting the argument and I have added the power instead. Any workaround?

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{mathtools}

\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}

\begin{document}

\[\cos{2x}\]
\[\cos^2{2x}\]

\end{document}

There is no problem with the first one, but once I add the square as I would normally it breaks

Mahoma
  • 675
  • For me the linked code works fine. Could you please provide a complete example that shows the issue? –  Nov 07 '18 at 03:06
  • 1
    For what it is worth, \sin does not take an argument. No braces {...} are needed. – Steven B. Segletes Nov 07 '18 at 04:45
  • I had no clue, I guess I have always assumed it took one and I always wrote it that way ´\sin{x}´ I've added a complete example – Mahoma Nov 07 '18 at 05:09
  • @StevenB.Segletes right, but I think that OP wants it for correct spacing when using an operator and braces. I add braces (manually) for that. – manooooh Nov 07 '18 at 05:59

1 Answers1

2

My (unrequested) advice would be not to introduce automatic brackets, and surely not to introduce automatically scaling brackets produced by \left...\right. Personally, I find \cos(x) rather clearer than \cos{x}, and you don't really spare keystrokes (and the braces are quite pointless, since \cos doesn't take an argument). As for \left/\right, there are enough examples on this site as why their overuse produces often rather bad results.

This being said, the following code (which should never be used under any circumstances :-)) might work as desired:

\documentclass{article}

\makeatletter
\newcommand*{\redefinesymbolwitharg}[1]{%
  \expandafter\let\csname ltx#1\expandafter\endcsname\csname #1\endcsname
  \@namedef{#1}{\@ifnextchar{^}{\@nameuse{#1@}}{\@nameuse{#1@}^{}}}%
  \expandafter\def\csname #1@\endcsname^##1##2{%
     \csname ltx#1\endcsname\ifx!##1!\else^{##1}\fi\mathopen{}\mathclose\bgroup\left(##2\aftergroup\egroup\right)
     }%
}
\makeatother

\begin{document}

\[
\cos{x} \quad \cos{\frac1x} \quad \cos^2{y} \quad \sin^{2\alpha+1}\theta
\]

\redefinesymbolwitharg{cos}
\redefinesymbolwitharg{sin}

\[
\cos{x} \quad \cos{\frac1x} \quad \cos^2{y} \quad \sin^{2\alpha+1}\theta
\]

\end{document}

enter image description here

campa
  • 31,130