4

I am wondering if it is possible to add the extra symbols in this example given the way I have written the code. I have provided a minimal example and another image with the additional symbols of + and times.

\documentclass{article}
\usepackage{tikz,mathtools}
\usetikzlibrary{tikzmark}
\usetikzlibrary{decorations.pathreplacing}

%%% Derivative Macro

\newcommand{\der}[2]{\dfrac{d#1}{d#2}}

%%% Derivative Prime Notation

\newcommand{\pder}[2]{#1^{\prime}(#2)}


\begin{document}

\begin{align*}
 \der{}{x} [\tikzmarknode{A}{(}2x^{5}+x-1\tikzmarknode{B}{)}\tikzmarknode{C} 
 {(}3x-2\tikzmarknode{D}{)}]&=\tikzmarknode{E}{(}2x^{5}+x-1\tikzmarknode{F} 
 {)}\tikzmarknode[red]{G}{(}\textcolor{red}{3}\tikzmarknode[red]{H} 
 {)}+\tikzmarknode{I}{(}3x-2\tikzmarknode{J}{)}\tikzmarknode[red]{K} 
 {(}\textcolor{red}{10x^{4}+1}\tikzmarknode[red]{L}{)}
 \end{align*}

\begin{tikzpicture}[overlay, remember picture]
\draw [decoration={brace},decorate,thick,blue]
 ([yshift=5pt]A.north) -- node[midway, above=3pt] {$f(x)$} 
 ([yshift=5pt]B.north);
\draw [decoration={brace},decorate,thick,blue]
 ([yshift=5pt]C.north) -- node[midway, above=3pt] {$g(x)$} 
  ([yshift=5pt]D.north);
 \draw [decoration={brace},decorate,thick,blue]
  ([yshift=5pt]E.north) -- node[midway, above=3pt] {$f(x)$} 
  ([yshift=5pt]F.north);
 \draw [decoration={brace},decorate,thick,red]
 ([yshift=5pt]G.north) -- node[midway, above=3pt] {$\pder{g}{x}$} 
 ([yshift=5pt]H.north);
 \draw [decoration={brace},decorate,thick,blue]
  ([yshift=5pt]I.north) -- node[midway, above=3pt] {$g(x)$} 
  ([yshift=5pt]J.north);
 \draw [decoration={brace},decorate,thick,red]
 ([yshift=5pt]K.north) -- node[midway, above=3pt] {$\pder{f}{x}$} 
 ([yshift=5pt]L.north);
 \end{tikzpicture}

 \end{document}

This outputs:

enter image description here

With the above braces I am trying add some additional operations: + and \cdot

enter image description here

g'(x) is a little smaller in the code example but if I can add the symbols and I can also add space to compensate later. Is it possible???

  • Do you really need a hammersledge like tikz for this? – Bernard Dec 5 '18 at 23:22
  • 1
    @Bernhard, it is the way I have been trained but I am interested in a "way" out if possible? – MathScholar Dec 5 '18 at 23:24
  • @Bernard If you are already using TikZ, then of course it's no overhead to use it for this. Likewise, if already using pstricks then your solution is great. One thing I love about this site is how people chime in with alternative solutions using different technologies. Although not always relevant to the OP, they are relevant to people looking for similar ideas. So please never think that just because the OP says "TikZ" that a psticks - or pure TeX - solution isn't welcome! (One of my most upvoted answers was a TikZ answer on an xy-matrix question.) – Loop Space Dec 6 '18 at 7:32
4

I would actually like to argue that it is more ergonomic to define a style that just inserts the brace path with the node on top, and not to have so many scattered \tikzmarknodes. This answer comes with an overbrace style, that can be used as

\draw[blue,thick,overbrace={A with {$f(x)$} called f1}];

where A is the node you want to overbrace, {$f(x)$} is the node on top and f1 its name. Then it is also much easier to insert the additional signs.

\documentclass{article}
\usepackage{tikz,mathtools}
\usetikzlibrary{tikzmark}
\usetikzlibrary{decorations.pathreplacing,calc}

%%% Derivative Macro

\newcommand{\der}[2]{\dfrac{\mathrm{d}#1}{\mathrm{d}#2}}

%%% Derivative Prime Notation

\newcommand{\pder}[2]{#1^{\prime}(#2)}

\tikzset{overbrace/.style args={#1 with #2 called #3}{
insert path={[decoration={brace},decorate] ([yshift=5pt]#1.north west)
--  node[midway, above=3pt] (#3) {#2}  
([yshift=5pt]#1.north east) }
}}

\begin{document}

\begin{align*}
 \der{}{x} [\tikzmarknode{A}{(2x^{5}+x-1)}
 \tikzmarknode{B}{\vphantom{x^{5}}(3x-2)}]
 &=\tikzmarknode{C}{(2x^{5}+x-1)}
 \tikzmarknode[red]{D}{\vphantom{x^{5}}(3H)}
 +\tikzmarknode{E}{\vphantom{x^{5}}(3x-2)}
 \tikzmarknode[red]{F}{(10x^{4}+1)}
 \end{align*}

\begin{tikzpicture}[overlay, remember picture]
\draw[blue,thick,overbrace={A with {$f(x)$} called f1}];
\draw[blue,thick,overbrace={B with {$g(x)$} called g1}];
\draw[blue,thick,overbrace={C with {$f(x)$} called f2}];
\draw[red,thick,overbrace={D with {$\pder{g}{x}$} called dg1}];
\draw[blue,thick,overbrace={E with {$g(x)$} called g2}];
\draw[red,thick,overbrace={F with {$\pder{f}{x}$} called df1}];
\path ($(A.east)!0.5!(B.west)$) coordinate (auxAB)
($(C.east)!0.5!(D.west)$) coordinate (auxCD)
($(D.east)!0.5!(E.west)$) coordinate (auxDE)
($(E.east)!0.5!(F.west)$) coordinate (auxEF);
\path (auxAB|-f1) node[blue]{$\cdot$}
([xshift=-3pt]auxCD|-f1) node[blue]{$\cdot$} %manual correction
(auxDE|-f1) node[blue]{$+$}
(auxEF|-f1) node[blue]{$\cdot$};
\end{tikzpicture}

\end{document}

enter image description here

If you want to layout of the braces, you only need to adjust the style. For instance, if you want gaps between them, you could do

\tikzset{overbrace/.style args={#1 with #2 called #3}{
insert path={[decoration={brace},decorate] ([yshift=5pt,xshift=1pt]#1.north west)
--  node[midway, above=3pt] (#3) {#2}  
([yshift=5pt,xshift=-1pt]#1.north east) }
}}

which yields

enter image description here

Note also that I put the signs on top of the signs of the underlying equation. If you want them in the middle, this will be even easier.

\documentclass{article}
\usepackage{tikz,mathtools}
\usetikzlibrary{tikzmark}
\usetikzlibrary{decorations.pathreplacing}

%%% Derivative Macro

\newcommand{\der}[2]{\dfrac{\mathrm{d}#1}{\mathrm{d}#2}}

%%% Derivative Prime Notation

\newcommand{\pder}[2]{#1^{\prime}(#2)}

\tikzset{overbrace/.style args={#1 with #2 called #3}{
insert path={[decoration={brace},decorate] ([yshift=5pt,xshift=1pt]#1.north west)
--  node[midway, above=3pt] (#3) {#2}  
([yshift=5pt,xshift=-1pt]#1.north east) }
}}

\begin{document}

\begin{align*}
 \der{}{x} [\tikzmarknode{A}{(2x^{5}+x-1)}
 \tikzmarknode{B}{\vphantom{x^{5}}(3x-2)}]
 &=\tikzmarknode{C}{(2x^{5}+x-1)}
 \tikzmarknode[red]{D}{\vphantom{x^{5}}(3)}
 +\tikzmarknode{E}{\vphantom{x^{5}}(3x-2)}
 \tikzmarknode[red]{F}{(10x^{4}+1)}
 \end{align*}

\begin{tikzpicture}[overlay, remember picture]
\draw[blue,thick,overbrace={A with {$f(x)$} called f1}];
\draw[blue,thick,overbrace={B with {$g(x)$} called g1}];
\draw[blue,thick,overbrace={C with {$f(x)$} called f2}];
\draw[red,thick,overbrace={D with {$\pder{g}{x}$} called dg1}];
\draw[blue,thick,overbrace={E with {$g(x)$} called g2}];
\draw[red,thick,overbrace={F with {$\pder{f}{x}$} called df1}];
\path[blue] (f1) -- (g1) node[midway] {$\cdot$}
(f2) -- (dg1)  node[midway] {$\cdot$}
(dg1) -- (g2)  node[midway] {$+$}
(g2) -- (df1)  node[midway] {$\cdot$};
\end{tikzpicture}

\end{document}

enter image description here

  • I guess it is ! I think you did not want the "H" in the post. I would also space the braces so they do not touch each other. I like the style technique which is making me look like a real rookie! :) – MathScholar Dec 5 '18 at 23:27
  • @MathScholar Sorry about the H. It is gone in the last option (which I was writing when I saw your comment.) The lower two options have gaps. – user121799 Dec 5 '18 at 23:32
  • thank you for this. I will have to play around with this and make things fit for the example in the code. I did not think it was possible! You proved me wrong – MathScholar Dec 5 '18 at 23:34
2

Despite being a very powerful tool, tikz can be too costly for some tasks. Here I use \overbrace and a small hack to add the operators while occupying 0pt width using a \makebox[0pt]{..} macro. Also, as @LoopSpace suggested, we can make the braces at the same height by adding \vphantom{x^5} to parentheses without that term.

\documentclass{article}
\usepackage{amsmath}
\begin{document}

\newcommand{\hide}[2]{\makebox[0pt][l]{$\hspace{#1pt}#2$}}

\[
\frac{d}{dx}[\overbrace{(2x^5+x-1)}^{f(x)\hide{20}{\cdot}} \overbrace{(\vphantom{x^5}3x-2)}^{g(x)}]
= \overbrace{(2x^5+x-1)}^{f(x)\hide{15}{\cdot}} \overbrace{(\vphantom{x^5}3)}^{g'(x)\hide{2}{+}} 
+ \overbrace{(\vphantom{x^5}3x-2)}^{g(x)\hide{10}{\cdot}} \overbrace{(10x^4+1)}^{f'(x)}
\]

\end{document}

enter image description here

  • 1
    Loath as I am to discourage use of tikzmark, I think it is really useful to see what can be achieved with simpler stuff. I do think it could do with the \vphantom{x^5} as in marmot's answer to get the braces level. – Loop Space Dec 5 '18 at 23:58
  • Sure, that improves the look of the equation, I added this, thanks. – AboAmmar Dec 6 '18 at 0:08
2

For fun, another simple solution with the \overbracecommand and pstricks:

\documentclass{article}
\usepackage{mathtools, esdiff}
\usepackage{pst-node}

\begin{document}

\begin{align*}
 \diff{}{x}\bigl[(\textcolor{red}{\overbrace{\strut\color{black}2x^{5}+x-1}^{\Rnode{f}{\textstyle f(x)}} \textcolor{black}{ )(}\overbrace{\strut\color{black}3x-2}^{\Rnode{g}{\textstyle g(x)}}})\bigr] &= (\textcolor{blue}{\overbrace{\color{black}2x^{5}+x-1}^{\Rnode{bluef}{\textstyle f(x})}})\textcolor{red}{\overbrace{\strut(3)}^{\Rnode{dg}{\textstyle g'(x)}} }+ (\textcolor{blue}{\overbrace{\strut\color{black}3x-2}^{\Rnode{blueg}{\textstyle g(x)}}} )\textcolor{red}{(\overbrace{10x^{4}+1}^{\Rnode{df}{\textstyle f'(x )}})}%
 %% Additional symbols
 \psset{linestyle=none}
 \ncline{f}{g}\ncput{\boldsymbol\cdot}
 \ncline{bluef}{dg}\ncput{\boldsymbol\cdot}
 \ncline{blueg}{df}\ncput{\boldsymbol\cdot}
 \ncline{blueg}{dg}\ncput{ + }
 \end{align*}

 \end{document} 

enter image description here

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.