Tell me more ×
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

4 Answers

up vote 4 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

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

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

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

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.