4

I need to strike out terms in equations. Based on the answers to this question, I've tried the cancel package, but the appearance of the resulting output does not convince me. Consider this example:

\documentclass{article}
\usepackage{cancel}
\newcommand{\pd}[2]{\frac{\partial #1}{\partial #2}}
\begin{document}
\begin{equation}
  \cancelto{\mathrm{ignored}}{\pd{f}{x}} 
    + \cancelto{\mathrm{ignored}}{\pd{g}{y}} + \pd{h}{z}
\end{equation}
\end{document}

This gives the following output:

enter image description here

If I use the package option makeroom, I get:

enter image description here

This is obviously unsatisfactory. What I would like to be able to do:

  • Arrow shapes consistent with those used by TikZ.
  • Arrow that are parallel (I would imagine this is difficult to do, so I would be happy to specify the angles manually).
  • Ability to specify the anchor of the text at the arrow tip (so that the text could be anchored at the bottom center or bottom right, for example).
  • Ability to specify the font size of the text near the arrow tip (which I don't seem to be able to do with \cancelto).
  • Automatically add space between the terms similar to what makeroom does (nice to have, but not essential if I can change the font) size because I can fine-tune the spacing if necessary).

Thanks for your help.

user1362373
  • 2,859

1 Answers1

9

Here is a proposal which comes with some pgfkeys that allow you to adjust the angle, the font and the arrow at will. (EDIT: Fixed an angle, big thanks to user1362373!)

\documentclass{article}
\usepackage{cancel}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc}
\tikzset{CancelTo/.is family,
CancelTo,
angle/.initial=60,
name/.initial=tmp,
node/.style={},
arrow/.style={-latex}}
\newcommand{\CancelTo}[3][]{\bgroup\tikzset{CancelTo/.cd,#1}
\tikzmarknode{\pgfkeysvalueof{/tikz/CancelTo/name}}{#3}
\begin{tikzpicture}[overlay,remember picture]
\draw[/tikz/CancelTo/arrow] let \p1=($(\pgfkeysvalueof{/tikz/CancelTo/name}.north)-(\pgfkeysvalueof{/tikz/CancelTo/name}.south)$),\n1={0.5*\y1*cot(\pgfkeysvalueof{/tikz/CancelTo/angle})},
\n2={\y1/sin(\pgfkeysvalueof{/tikz/CancelTo/angle})} 
in ([xshift=-\n1]\pgfkeysvalueof{/tikz/CancelTo/name}.south) -- ++ (\pgfkeysvalueof{/tikz/CancelTo/angle}:\n2+5pt) 
node[above right,/tikz/CancelTo/node] (tmplabel) {#2};
\path let \p1=($(tmplabel.north east)-(\pgfkeysvalueof{/tikz/CancelTo/name}.east)$) in
\pgfextra{\xdef\mydist{\x1}};
\end{tikzpicture}\egroup\vphantom{\cancelto{#2}{#3}}\hspace{\mydist}}
\newcommand{\pd}[2]{\frac{\partial #1}{\partial #2}}
\begin{document}
An equation.
\begin{equation}
    \CancelTo{ignored}{\pd{f}{x}} 
  \CancelTo[arrow/.style={-stealth}]{ignored}{\pd{f}{x}} 
    + \CancelTo[node/.style={font=\tiny,text=blue}]{ignored}{\pd{g}{y}} + \pd{h}{z}
\end{equation}
Another equation with another angle.
\begin{equation}
    \CancelTo[angle=70]{ignored}{\pd{f}{x}} 
  \CancelTo[angle=70,name=mynode]{ignored}{\pd{f}{x}} 
    + \CancelTo[angle=70]{ignored}{\pd{g}{y}} + \pd{h}{z}
\end{equation}
Bonus: you can name nodes and use \tikzmarknode{this}{this} otherwise.
\begin{tikzpicture}[overlay,remember picture]
\draw[-latex,red] (this) to[bend right] (mynode);
\end{tikzpicture}
\end{document}

enter image description here

  • Thanks, this does mostly what I need, I can take care of some of the fine-tuning. One thing that would be nice would be for the vertical locations of the top and bottom of the arrows to be consistent. Now the bottom of the arrow for the dg/dy term is lower than for the df/dx term. – user1362373 Jan 14 '19 at 06:41
  • @user1362373 Yes, this is because y goes below the baseline whereas x does not. Would it be acceptable to just insert a \vphantom{y} in the derivatives w.r.t. x? One way to achieve this is to use \newcommand{\pd}[2]{\frac{\partial #1\vphantom{y}}{\partial #2\vphantom{y}}}. –  Jan 14 '19 at 12:19
  • In \n2={\y1/sin(60), shouldn't you also use \pgfkeysvalueof{/tikz/CancelTo/angle}? – user1362373 Jan 17 '19 at 06:01
  • @user1362373 Absolutely! Thanks a lot! –  Jan 17 '19 at 06:08
  • Another question: In \tikzmarknode{\pgfkeysvalueof{/tikz/CancelTo/name}}{#3}, I assume you're assigning the value of the third argument to the angle. But in that case, why doesn't it say /tikz/CancelTo/angle? (I'm asking because I'm trying to add further optional arguments to fine-tune the spacing, but failing miserably, so I'm trying to understand the details of what you've done.) – user1362373 Jan 17 '19 at 06:18
  • @user1362373 No, \pgfkeysvalueof{/tikz/CancelTo/name} yields the name of the shape. Nodes must have names. By default, all nodes get the name unless you say otherwise. You can add additional keys under \tikzset{CancelTo/.is family, CancelTo, ..., which may help to fine-tune. –  Jan 17 '19 at 06:22
  • So the [3], which I thought indicated the first optional argument, is actually immaterial as far as me adding additional options is concerned. It seems that I can just name the option under tikzset and then add it in the [] after \CancelTo command, at least that seems to work. – user1362373 Jan 17 '19 at 08:34
  • @user1362373 [3] is the number of arguments. [] indicates that the first one is an option, and the default is empty. So the syntax is \CancelTo[<pgf keys>]{<text at the end of arrow>}{<object that is to be canceled>}. –  Jan 17 '19 at 15:30