Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to strikeout an equation with diagonal line but I've not managed to get the diagonal line to draw as I want. The closest I've gotten is via the first example in Frédéric's answer of \cancel draws under thing being canceled:

\documentclass{minimal}

\usepackage{tikz}

\newcommand{\hcancel}[1]{%
    \tikz[baseline=(tocancel.base)]{
        \node[inner sep=0pt,outer sep=0pt] (tocancel) {#1};
        \draw[red] (tocancel.south west) -- (tocancel.north east);
    }%
}%

\begin{document}

\begin{equation}\label{eq:2}
\hcancel{$h_1 \land h_2 \land h_3 \land h_4 \land h_5 \land h_6 \land h_7 \land h_8 \land h_9 \land h_{10}$}
\end{equation}

\end{document}

This produces:

Line starts too low and ends too high

But the line starts too low and ends too high. The following is what I want:

The line as I want it to draw

I'd be happy for an option to make the line start slightly earlier and end slightly later too.

share|improve this question
add comment

5 Answers

up vote 7 down vote accepted

You can modify Frédéric's code so that \hcancel receives four more mandatory arguments controlling the vertical and horizontal shifting for the starting and ending points:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\hcancel}[5]{%
    \tikz[baseline=(tocancel.base)]{
        \node[inner sep=0pt,outer sep=0pt] (tocancel) {#1};
        \draw[red] ($(tocancel.south west)+(#2,#3)$) -- ($(tocancel.north east)+(#4,#5)$);
    }%
}%

\begin{document}

\begin{equation}\label{eq:1}
\hcancel{$h_1 \land h_2 \land h_3 \land h_4 \land h_5 \land h_6 \land h_7 \land h_8 \land h_9 \land h_{10}$}{0pt}{0pt}{0pt}{0pt}
\end{equation}

\begin{equation}\label{eq:2}
\hcancel{$h_1 \land h_2 \land h_3 \land h_4 \land h_5 \land h_6 \land h_7 \land h_8 \land h_9 \land h_{10}$}{-3pt}{3pt}{3pt}{-2pt}
\end{equation}

\end{document}

The new syntax:

\hcancel{<text>}{<start. point horiz. shifting>}{<start. point vertical shifting>}{<end. point horiz. shifting>}{<end. point vertical shifting>}

EDIT: using the xparse package, the definition of the new command is much more flexible; using something like

\usepackage{xparse}
\DeclareDocumentCommand{\hcancel}{mO{0pt}O{0pt}O{0pt}O{0pt}}{%
    \tikz[baseline=(tocancel.base)]{
        \node[inner sep=0pt,outer sep=0pt] (tocancel) {#1};
        \draw[red] ($(tocancel.south west)+(#2,#3)$) -- ($(tocancel.north east)+(#4,#5)$);
    }%
}%

allows the use of \hcancel{<text>} for the standard behaviour of the command as defined by Frédéric and to use the four (now optional) arguments to control the horizontal/vertical shifting:

\documentclass{article}
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{calc}

\DeclareDocumentCommand{\hcancel}{mO{0pt}O{0pt}O{0pt}O{0pt}}{%
    \tikz[baseline=(tocancel.base)]{
        \node[inner sep=0pt,outer sep=0pt] (tocancel) {#1};
        \draw[red] ($(tocancel.south west)+(#2,#3)$) -- ($(tocancel.north east)+(#4,#5)$);
    }%
}%

\begin{document}

\begin{equation}\label{eq:1}
\hcancel{$h_1 \land h_2 \land h_3 \land h_4 \land h_5 \land h_6 \land h_7 \land h_8 \land h_9 \land h_{10}$}
\end{equation}

\begin{equation}\label{eq:2}
\hcancel{$h_1 \land h_2 \land h_3 \land h_4 \land h_5 \land h_6 \land h_7 \land h_8 \land h_9 \land h_{10}$}[-3pt][3pt][3pt][-2pt]
\end{equation}

\end{document}
share|improve this answer
add comment

You can create nodes at the beginning and the end of the line, and shift them vertically:

\tikzstyle{nosep}=[inner sep=0pt, outer sep=0pt]
\newcommand{\hcancel}[1]{%
    \tikz[baseline=(tocancel.base)]{
        \node[nosep] (tocancel) {#1};
        \node[nosep, yshift=.5ex]  (from) at (tocancel.south west) {};
        \node[nosep, yshift=-.5ex] (to)   at (tocancel.north east) {};
        \draw[red] (from) -- (to);
    }%
}%

You can find the best shifts by trial and error. I chose .5ex and -.5ex arbitrarily.

share|improve this answer
1  
@N.N. And to make the line slightly longer, add xshift=-1ex to the from node (+1ex for the to node). –  Alan Munn Jun 13 '11 at 21:05
add comment

Another possibility is to create a style.

\documentclass[]{scrartcl}
\usepackage{tikz}
\usetikzlibrary{calc}  


\begin{document}

\begin{tikzpicture}[cancel/.style={path picture={ \draw[#1]
($ (path picture bounding box.south west)+(-3pt,6pt)$) -- ($(path picture bounding box.north east)+(3pt,-6pt)$);
}}]  
\node   [inner sep=3pt,cancel=red] {$2x+3=y$};
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
    
It's possible to place the code in a macro with arguments. –  Alain Matthes Jun 13 '11 at 21:53
    
When I try to compile this I get: ! Package pgfkeys Error: I do not know the key '/tikz/path picture' and I am going to ignore it. Perhaps you misspelled it. –  N.N. Jun 14 '11 at 7:33
1  
@N.N. I use pgf 2.1. and perhaps 'path picture' is defined since the 2.1 but I don't know :( I use this option in several examples and in some answers without problem. –  Alain Matthes Jun 14 '11 at 8:03
    
OK, you're probably right. I've got pgf 2.0 from TeX Live 2009 on my system. –  N.N. Jun 14 '11 at 8:07
add comment

This is thought as enhancement of Gonzalo’s answer. The more arguments a command gets, the easier one can lose track of them. Here comes the key-value approach to aid.

Therefore I want to show a definition using the package keycommand. It could also be done with xparse and l3keys, but I am used to keycommand; in my eyes it is also more suited for end users, at least for cases like this. Note, though, that there is a bug in, and you must include the patch provided by Joseph Wright in his answer to the question How do I use \ifcommandkey , or how do I check if a key was given? after the package was loaded (actually you would need this only in cases, when you use the command \ifcommandkey).

Note, that I additionally added a key to change the line color, used in third example.

\documentclass{article}
\usepackage{keycommand}
% Patch by Joseph Wright ("bug in the definition of \ifcommandkey (2010/04/27 v3.1415)"),
% http://tex.stackexchange.com/a/35794
\begingroup
  \makeatletter
  \catcode`\/=8 %
  \@firstofone
    {
      \endgroup
      \renewcommand{\ifcommandkey}[1]{%
        \csname @\expandafter \expandafter \expandafter
        \expandafter \expandafter \expandafter \expandafter
        \kcmd@nbk \commandkey {#1}//{first}{second}//oftwo\endcsname
      }
    }
%--------%
\usepackage{tikz}
\usetikzlibrary{calc}
\newkeycommand{\hcancel}[hshiftstart=0pt,vshiftstart=0pt,hshiftend=0pt,vshiftend=0pt,color=red][1]{%
  \tikz[baseline=(tocancel.base)]{
        \node[inner sep=0pt,outer sep=0pt] (tocancel) {#1};
        \draw[\commandkey{color}] ($(tocancel.south west)+(\commandkey{hshiftstart},\commandkey{vshiftstart})$) --
                                  ($(tocancel.north east)+(\commandkey{hshiftend},\commandkey{vshiftend})$);
  }%
}%

\begin{document}

\begin{equation}\label{eq:1}
\hcancel{$h_1 \land h_2 \land h_3 \land h_4 \land h_5 \land h_6 \land h_7 \land h_8 \land h_9 \land h_{10}$}
\end{equation}

\begin{equation}\label{eq:2}
\hcancel[hshiftstart=-3pt,vshiftstart=0.5em,hshiftend=3pt,vshiftend=-0.5em]
{$h_1 \land h_2 \land h_3 \land h_4 \land h_5 \land h_6 \land h_7 \land h_8 \land h_9 \land h_{10}$}
\end{equation}

\begin{equation}\label{eq:3}
\hcancel[hshiftstart=-3pt,vshiftstart=1em,hshiftend=3pt,vshiftend=-1em,color=blue]
{$h_1 \land h_2 \land h_3 \land h_4 \land h_5 \land h_6 \land h_7 \land h_8 \land h_9 \land h_{10}$}
\end{equation}

\end{document}

example output

share|improve this answer
add comment

I use PSTricks in this answer. Please adjust the parameters until they suit your preference best. The parameters given in the code below are self-explanatory.

enter image description here

\documentclass{minimal}

\usepackage{pstricks-add}
\psset{linecolor=red}
\def\myeq{\psDefBoxNodes{A}{h_1 \land h_2 \land h_3 \land h_4 \land h_5 \land h_6 \land h_7 \land h_8 \land h_9 \land h_{10}}}
\begin{document}

\centering

Your settings:
\begin{equation}
\myeq
\ncline[nodesep=3pt,offsetA=-1pt,offsetB=3pt]{A:bl}{A:tr}
\end{equation}
\\[5mm]

Controlling the length:
\begin{equation}
\myeq
\ncline[nodesep=10pt,offsetA=-1pt,offsetB=3pt]{A:bl}{A:tr}
\end{equation}
\\[5mm]

Controlling the left node:
\begin{equation}
\myeq
\ncline[nodesep=3pt,offsetA=-1pt,offsetB=-10pt]{A:bl}{A:tr}
\end{equation}
\\[5mm]


Controlling the right node:
\begin{equation}
\myeq
\ncline[nodesep=3pt,offsetA=10pt,offsetB=3pt]{A:bl}{A:tr}
\end{equation}

\end{document}
share|improve this answer
    
Just as remark: See Why should the minimal class be avoided?. –  Speravir Jan 29 at 18:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.