Abnormally stretched arrow inside environment
No need to reinvent the wheel, the tcolorbox
package allows you to create a box that does what you want.
Output:
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{tikz}
\usepackage{environ}
\usepackage{fancyhdr}
\usepackage{tasks}
\pagestyle{empty}
\usepackage[most]{tcolorbox}
% title style "solution"
\tcbset{titleresume/.style={boxed title style={colframe=black,colbacktitle=white,arc=3pt,boxrule=.6pt}}}
% box creating the box "solution"
\newtcolorbox{solution}{
titleresume,arc=7pt,width=0.95\textwidth,
colframe=orange,colbacktitle=white,coltitle=black,colback=white,fonttitle=\sl\bfseries\large,
center,boxrule=1.6pt,%boxsep=10pt,
enhanced,nobeforeafter,
attach boxed title to top left={yshift=-3mm,xshift=5pt},
title={Solution}}
\tikzset{
main node/.style={inner sep=0,outer sep=0},
label node/.style={inner sep=0,outer ysep=.2em,outer xsep=.4em,font=\scriptsize,overlay},
strike out/.style={shorten <=-.2em,shorten >=-.5em,overlay}
}
\newcommand{\cancelto}[3][]{\tikz[baseline=(N.base)]{
\node[main node](N){$#2$};
\node[label node,#1, anchor=south west] at (N.north east){$#3$};
\draw[strike out,-latex,#1] (N.south west) -- (N.north east);
}}
\newcommand{\bcancelto}[3][]{\tikz[baseline=(N.base)]{
\node[main node](N){$#2$};
\node[label node,#1, anchor=north west] at (N.south east){$#3$};
\draw[strike out,-latex,#1] (N.north west) -- (N.south east);
}}
\begin{document}
\begin{align*}
\text{This is good:}\qquad \cancelto[red]{2x-1}{1}
\end{align*}
\begin{solution}
\begin{align*}
\text{This is good too:}\qquad \cancelto[red]{2x-1}{1}
\end{align*}
\end{solution}
\end{document}
One way to work around this issue is to use a box and typeset the contents into that:
References:
- Use
tikzset
instead of\tikzstyle
: Should \tikzset or \tikzstyle be used to define TikZ styles? - I also replaced the
center
environment with\centering
as that results in less vertical spacing.
Code:
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{tikz}
\usepackage{environ}
\usepackage{fancyhdr}
\usepackage{tasks}
\pagestyle{empty}
\tikzset{fancytitle3/.style={draw=black, fill=white, text=black, rounded corners = 3pt, inner sep = 3}}
\newsavebox{\MyBox}
\NewEnviron{sol}{%
\savebox{\MyBox}{%
\begin{minipage}{.95\textwidth}
\begin{small}\textsl{\BODY}\end{small}
\end{minipage}%
}%
\centering
\begin{tikzpicture}
\node[rectangle, rounded corners = 7pt, inner sep = 10 ,minimum width=0.9\textwidth,
text opacity=1,
draw=orange, ultra thick, draw opacity=1,
fill=white, fill opacity=1] (box)
{%
\usebox{\MyBox}%
};
\node[fancytitle3, right=5pt] at (box.north west) {\textsl{\textbf{\begin{small}Solution \end{small}} }};
\end{tikzpicture}%
}
\tikzset{
main node/.style={inner sep=0,outer sep=0},
label node/.style={inner sep=0,outer ysep=.2em,outer xsep=.4em,font=\scriptsize,overlay},
strike out/.style={shorten <=-.2em,shorten >=-.5em,overlay}
}
\newcommand{\cancelto}[3][]{%
\begingroup
\tikz[baseline=(N.base)]{
\node[main node](N){$#2$};
\node[label node,#1, anchor=south west] at (N.north east){$#3$};
\draw[strike out,-latex,#1] (N.south west) -- (N.north east);
}%
\endgroup
}
\newcommand{\bcancelto}[3][]{%
\tikz[baseline=(N.base)]{
\node[main node](N){$#2$};
\node[label node,#1, anchor=north west] at (N.south east){$#3$};
\draw[strike out,-latex,#1] (N.north west) -- (N.south east);
}}
\begin{document}
\begin{align*}
\text{This is good:}\qquad \cancelto[red]{2x-1}{1}
\end{align*}
\begin{sol}
\begin{align*}
\text{This is now also good:}\qquad \cancelto[red]{2x-1}{1}
\end{align*}
\end{sol}
\end{document}