1

This is a follow-up question to my earlier question about Improving typesetting of striking out terms in equations. The solution proposed by @marmot works well, but I have to make more manual adjustments to the spacing than I expected. The fundamental problem appears to be that the label at the top of the arrow that strikes out a term influences the spacing between the terms. This manifests in two problems shown in the following picture:

enter image description here

The above was generated with the following (not quite minimal) MWE. It is basically equal to the proposed solution with some additions to allow me to adjust the position of the tip of the arrow and the position of the text relative to the tip of the arrow.

\documentclass{article}
\usepackage{cancel}
\usepackage{tikz}
\usetikzlibrary{calc,positioning,tikzmark}
\tikzset{CancelTo/.is family,
CancelTo,
angle/.initial=60,
name/.initial=tmp,
node/.style={},
arrow/.style={->},
topoffs/.initial=2pt, 
nodeaboveoffs/.initial=1pt,
nodeleftoffs/.initial=0pt
}
\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+\pgfkeysvalueof{/tikz/CancelTo/topoffs}) 
  node[above left=\pgfkeysvalueof{/tikz/CancelTo/nodeaboveoffs} and \pgfkeysvalueof{/tikz/CancelTo/nodeleftoffs},inner sep=0pt,font=\scriptsize,/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}
\textbf{Problem 1 (``narrow" term):}
With zero offset:
\begin{equation}
  \CancelTo[nodeleftoffs=0pt]{ignore}{\pd{f}{x}} +
  \pd{g}{y} = 0
\end{equation}
With offset of $-11$pt:
\begin{equation}
  \CancelTo[nodeleftoffs=-11pt]{ignore}{\pd{f}{x}} +
  \pd{g}{y} = 0
\end{equation}
What I would like to get (spacing between first fraction and $+$ unaffected): 
\begin{equation}
  \CancelTo[nodeleftoffs=-11pt]{ignore}{\pd{f}{x}} \!\!\!\!\!\! + 
  \pd{g}{y} = 0
\end{equation}
\textbf{Problem 2 (``wide" term):}
Equation without arrow:
\begin{equation}
  (a+b)\pd{f}{x} + \pd{g}{y} = 0
\end{equation}
Equation with arrow and zero offset:
\begin{equation}
  \CancelTo[nodeleftoffs=0pt]{ignore}{(a+b)\pd{f}{x}} +
  \pd{g}{y} = 0
\end{equation}
What I would like to get (spacing between first fraction and $+$ unaffected): 
\begin{equation}
  \CancelTo[nodeleftoffs=-11pt]{ignore}{(a+b)\pd{f}{x}} \;\, +
  \pd{g}{y} = 0
\end{equation}
\end{document}

Having said all this, my question is the following: How can I strike out a term and add text at the tip of the arrow without changing the spacing in the equation?

3

I added an overlay option to the keys which makes adding the space by hand unnecessary. (Instead of the distances you are using, you may also consider just to adjust the anchor of the label node.)

\documentclass{article}
\usepackage{cancel}
\usepackage{tikz}
\usetikzlibrary{calc,positioning,tikzmark}
\newif\ifCancelToOverlay
\tikzset{CancelTo/.is family,
CancelTo,
angle/.initial=60,
name/.initial=tmp,
node/.style={},
arrow/.style={->},
topoffs/.initial=2pt, 
nodeaboveoffs/.initial=1pt,
nodeleftoffs/.initial=0pt,
overlay/.is if=CancelToOverlay
}
\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+\pgfkeysvalueof{/tikz/CancelTo/topoffs}) 
  node[above left=\pgfkeysvalueof{/tikz/CancelTo/nodeaboveoffs} and \pgfkeysvalueof{/tikz/CancelTo/nodeleftoffs},inner sep=0pt,font=\scriptsize,/tikz/CancelTo/node] (tmplabel) {#2};
  \path let \p1=($(tmplabel.north east)-(\pgfkeysvalueof{/tikz/CancelTo/name}.east)$) in
  \pgfextra{\ifCancelToOverlay
  \xdef\mydist{0pt}
  \else 
  \xdef\mydist{\x1}
  \fi};
\end{tikzpicture}\egroup\vphantom{\cancelto{#2}{#3}}\hspace{\mydist}}
\newcommand{\pd}[2]{\frac{\partial #1}{\partial #2}}
\begin{document}
\textbf{Problem 1 (``narrow" term):}
With zero offset:
\begin{equation}
  \CancelTo[nodeleftoffs=0pt]{ignore}{\pd{f}{x}} +
  \pd{g}{y} = 0
\end{equation}
With offset of $-11$pt:
\begin{equation}
  \CancelTo[nodeleftoffs=-11pt]{ignore}{\pd{f}{x}} +
  \pd{g}{y} = 0
\end{equation}
What I would like to get (spacing between first fraction and $+$ unaffected): 
\begin{equation}
  \CancelTo[nodeleftoffs=-11pt,overlay=true]{ignore}{\pd{f}{x}} + 
  \pd{g}{y} = 0
\end{equation}
\textbf{Problem 2 (``wide" term):}
Equation without arrow:
\begin{equation}
  (a+b)\pd{f}{x} + \pd{g}{y} = 0
\end{equation}
Equation with arrow and zero offset:
\begin{equation}
  \CancelTo[nodeleftoffs=0pt]{ignore}{(a+b)\pd{f}{x}} +
  \pd{g}{y} = 0
\end{equation}
What I would like to get (spacing between first fraction and $+$ unaffected): 
\begin{equation}
  \CancelTo[nodeleftoffs=-11pt,overlay=true]{ignore}{(a+b)\pd{f}{x}} +
  \pd{g}{y} = 0
\end{equation}
\end{document}

enter image description here

  • This is great, but doesn't it make more sense to make overlay the default? Also, I agree that changing the node anchor can help, but it doesn't eliminate the basic problem (I played around with it before asking the follow-up question.) – user1362373 Jan 17 '19 at 17:31
  • @user1362373 I guess the only one who can decide what should be default is you. In the previous answer I thought you want these spaces since the cancel version, together with makeroom, has them, too. If you add overlay=true after (say) overlay/.is if=CancelToOverlay, the default will be true but you can add overlay=false whenever you want to have the space inserted. – user121799 Jan 17 '19 at 17:38
  • 1
    No, this must have been a misunderstanding. But with your suggestion for making overlay the default, this is now exactly what I need. Thanks! – user1362373 Jan 17 '19 at 17:40

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.