0

Using DeclarePairedDelimiter from mathtools I have defined a command to enclose expressions with parenthesis.

\DeclarePairedDelimiter\p{(}{)}

Now, I want to defined a command for a function

\newcommand{\myfunc}[1]{f\p{#1}}

Is there an easy way to pass the optional parameter from \myfunc to \p so that I can write \myfunc[\Big]{x}. I know I can use \DeclarePairedDelimiterXPP to declare \myfunc from scratch but I was wondering if there was a way to make my definitions more modular.

MWE

\documentclass{article}
\usepackage{mathtools}
\DeclarePairedDelimiter\p{(}{)}
\newcommand{\myfunc}[1]{f\p{#1}}

\begin{document}
\[\myfunc{x}\]
% How to make these work? Without breaking \myfunc{x}
% \[\myfunc[\Big]{x}\] 
% \[\myfunc*{x}\] 
\end{document}
1

Just define \myfunc without arguments.

\documentclass{article}
\usepackage{mathtools}

\DeclarePairedDelimiter\p{(}{)}
\newcommand{\myfunc}{f\p}

\begin{document}

\begin{gather*}
\myfunc{x} \\
\myfunc[\Big]{x} \\
\myfunc*{\frac{x}{2}}
\end{gather*}

\end{document}

enter image description here

2

It is a lot easier to simply use \DeclarePairedDelimiterXPP, which is build for this kind of constructions. Just use

\DeclarePairedDelimiterXPP\myfunc[1]{f}{(}{)}{}{#1}

On the other hand, you you really want to use \p, then you'll need to manually parse the arguments using the xparse package, and then call \p in the appropriate manner using the detected options (you need to look for starred, optional in [] and mandatory, so in xparse you need to make a macro with an s o m 'signature').

2

I defined a macro that inserts the optional argument before the ( and ). The definition of the macro is made inside a local group so that once it exits, the effect of the \DeclarePairedDelimiter is erased so it can be used again in the document.

I also added the solution that @campa and @daleif provided in the comments, which is much cleaner and less prone to errors.

enter image description here

\documentclass{article}
\usepackage{mathtools}
\DeclarePairedDelimiter\p{(}{)}

\newcommand{\myfunc}[1]{f\p{#1}}

% Keeping my previous solution
\newcommand{\myotherfunc}[2][]{%
\begingroup% < Make the definition of \pp local to this group (thanks @campa)
\DeclarePairedDelimiter\pp{#1(}{#1)}
f\pp{#2}%
\endgroup%
}

% Easier, faster, cleaner solution by @campa and @daleif
\DeclarePairedDelimiterXPP\myanotherfunc[1]{f}{(}{)}{}{#1}

\begin{document}
\[\myfunc{x}\]
% How to make these work? Without breaking \myfunc{x}
\[\myotherfunc[\Big]{x} \text{ and } \myotherfunc[\Big]{y}\]
\[\myanotherfunc[\Big]{x} \text{ and } \myanotherfunc[\Big]{y}\] 
% \[\myfunc*{x}\] 
\end{document}

I just couldn't understand the purpose of the starred version.

  • @campa D= I didn't think about this. I'll try to fix it. – Phelype Oleinik Jan 22 '18 at 11:38
  • 2
    Campas comment above, it the best way to do this, simply use \DeclarePairedDelimiterXPP\p[1]{f}{(}{)}{}{#1}, job done. – daleif Jan 22 '18 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.