2

First I want to thank mathtools package maintainers.

Issue Description

The issue is about declaring paired delimiters with \DeclarePairedDelimiter(X): if I want to e.g. colour the delimiters (with defining a new command), the mathtools won't work.

MWE:

% ============================================================================ %
%                                   SETTINGS                                   %
% ============================================================================ %
\documentclass{article}

\usepackage{xcolor} % for \textcolor command
\usepackage{mathtools} % for \DeclarePairedDelimiterX command

% Formatted paired delimiter
%
%   - coloured delimiter definition
%
\newcommand{\absdelim}{\textcolor{red}{|}}
%
%   - declare the paired delimiter with mathtools
%
\DeclarePairedDelimiterX{\absval}[1]{\absdelim}{\absdelim}{#1}

% ============================================================================ %
%                                     BODY                                     %
% ============================================================================ %
\begin{document}

This does not work:
\[
    \absval*{\frac{\frac{a}{b}}{\frac{c}{d}}}
\]

This alone works:
\[
    \absdelim
\]

\end{document}

The compiler raises this: Missing delimiter (. inserted).

You can test with other custom commands: if the delimiter in the \newcommand{\absdelim} is formatted by another command, the compiler won't run.

Question

Is there a trick to format delimiters (perhaps using <post> \DeclarePairedDelimiterX's optional argument or by using \DeclarePairedDelimiterPPX command)?

Have a nice day!

2 Answers 2

3

You can redefine the inner wrapper (you probably will have to redefine the nostarnonscaled and nostarscaled wrapper too, see the documentation). \mathcolor requires a rather recent LaTeX.

\documentclass{article}

\usepackage{xcolor} % for \textcolor command
\usepackage{mathtools} % for \DeclarePairedDelimiterX command

\newcommand{\absdelim}{|}

\DeclarePairedDelimiterX\absval[1]{\absdelim}{\absdelim}{#1}
\reDeclarePairedDelimiterInnerWrapper\absval{star}{\mathcolor{red}{#1}#2\mathcolor{green}{#3}}
\begin{document}

\[
    \absval*{\frac{\frac{a}{b}}{\frac{c}{d}}}
\]

\end{document}

enter image description here

4
  • Thank! I accept this as the answer because it works for colour and I prefer using the command that redefines the inner wrapper than re-changing the text colour in black (several drawbacks to this). However, my original idea was not only to change the colour but add a glossary entry link onto the delimiters, but this is another question (it does not work because of \glshyperlink command behaviour)
    – vepain
    Sep 21, 2022 at 9:55
  • 1
    As David wrote, \left and \right don't like to be in a group so you are quite restricted in what you can put around them (\textcolor in my example wouldn't work because of this.) Sep 21, 2022 at 10:01
  • @vepain instead of black in mine you could use a saved color using \colorlet from xcolor Sep 21, 2022 at 10:03
  • @UlrikeFischer I see, the issue is on \left \right behaviour, is there a more flexible way to simulate \left \right with formatted delimiters (hyper-linkable, coloured etc.) ? If there is no way, I will use manual delimiter sizes (e.g. \big). --- @DavidCarlisle yes, this is what I actually do
    – vepain
    Sep 21, 2022 at 11:42
1

\left and \right need to be in the same group, so you can not do

{\color{red}\left|} ....    {\color{red}\right|} 

but you can do

   {\color{red}\left| {\color{black} ....    }\right|} 

enter image description here

\documentclass{article}

\usepackage{xcolor} % for \textcolor command
\usepackage{mathtools} % for \DeclarePairedDelimiterX command

% Formatted paired delimiter
%
%   - coloured delimiter definition
%

%
%   - declare the paired delimiter with mathtools
%
\DeclarePairedDelimiterX{\absvalblack}[1]{|}{|}{#1}
\NewDocumentCommand\absval{sm}{%
 \mathcolor{red}{%
   \IfBooleanTF{#1}{\absvalblack*}{\absvalblack}{\mathcolor{black}{#2}}%
  }}
% ============================================================================ %
%                                     BODY                                     %
% ============================================================================ %
\begin{document}

This does work:
\[
    \absval*{\frac{\frac{a}{b}}{\frac{c}{d}}}
\]


\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .