7

I am trying to use the solution offered by Kpym in this topic.

\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}


\tikzstyle{fancytitle3}=[draw=black, fill=white, text=black, rounded corners = 3pt, inner sep = 3]
\NewEnviron{sol}{%
 \begin{center}
 \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)
    {%
        \begin{minipage}{.95\textwidth}
         \begin{small}\textsl{\BODY}\end{small}
        \end{minipage}
    };
     \node[fancytitle3, right=5pt] at (box.north west) {\textsl{\textbf{\begin{small}Solution \end{small}} }};
\end{tikzpicture}
\end{center}
}
\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{sol}
\begin{align*}
    \text{We don't want this:}\qquad \cancelto[red]{2x-1}{1}
\end{align*}
\end{sol}
\end{document}

enter image description here

As you can see, when I use the \cancelto inside the solution environment, something is telling tex to stretch the arrow and I'm not sure what is causing it.

I am aware of the cancel package, which doesn't exhibit this problem. At this stage, I am more interested in whether current situation can be mended.

ShreevatsaR
  • 45,428
  • 10
  • 117
  • 149
AlvinL
  • 225
  • You have nested tikzpicture environments, which is a no-no... The sol environemnt uses puts its content within a tikzpicture and then the \cancelto invokes another tikzpicture (via the \tikz). – Peter Grill Apr 24 '18 at 07:59
  • @PeterGrill a no-no it is, then. Perhaps, I'd have more luck modifying the existing cancel commands. – AlvinL Apr 24 '18 at 08:01
  • Not necessarily... wait... – Peter Grill Apr 24 '18 at 08:05

2 Answers2

6

No need to reinvent the wheel, the tcolorbox package allows you to create a box that does what you want.

Output:

enter image description here

\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}
AndréC
  • 24,137
5

One way to work around this issue is to use a box and typeset the contents into that:

enter image description here

References:

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)]{ \nodemain node{$#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)]{ \nodemain node{$#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}

Peter Grill
  • 223,288
  • Interesting, do you use a box to make it look like it's not nesting tikzpictures? – AlvinL Apr 24 '18 at 08:11
  • 1
    @AlvinLepik: Yeah, when the box is being typeset it is not in a tikzpicture (Note that the box is typeset before \begin{tikzpicture}) . Then in the tikzpicture I just use the already typset contents of the box. – Peter Grill Apr 24 '18 at 08:12