5

How to draw number line and hatching with TikZ like the following?

enter image description here

kalakay
  • 2,469

2 Answers2

7

Just for typing exercise with PSTricks. Without simplification, it contains 487 characters.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-eucl}
\begin{document}
\begin{pspicture}(-4,-.5)(7.5,1)
    \psset{arrows=->}
    \pstGeonode[PointName={-3,-2,4,6},PosAngle=-90](-3,0){A}(-2,0){B}(4,0){C}(6,0){D}
    \uput{1.2}[0](D){$x$}
    \ncline[nodesep=-1]{A}{D}
    \psline(A)(A|0,1)([nodesep=-1,offset=1]A)
    \psframe[fillstyle=hlines](C)(D|0,.5)
    \psline{o-o}(B)(B|0,.5)(D|0,.5)(D)
    \psline(C)(C|0,1)([nodesep=3,offset=1]C)
\end{pspicture}
\end{document}

enter image description here

To get bigger dots and arrows, replace the \psset{arrows=->} with \psset{arrows=->,dotscale=2,arrowscale=2}. So the output becomes as follows.

enter image description here

  • 4
    I don't know why there is all this fuss about the number of characters. The OP has got two solutions by typing only 66 characters. A clear winner. – Mark Wibrow Nov 05 '13 at 15:34
  • @MarkWibrow: It is just an additional constraint to be more challenging. IMHO, the fewer keystrokes we use in the code, the more challenging the problem becomes. – kiss my armpit Nov 05 '13 at 15:44
5

Simpliest tikz solution:

\documentclass[varwidth,border=3pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{patterns}

\begin{document}
\begin{tikzpicture}
    \fill[even odd rule,pattern=north east lines,pattern color=black!30] (4, 0) rectangle (6, 1);
    \draw[->] (-4, 0) -- (8, 0);
    \draw (-2, 0) |- (4, 1) -| (6, 0);
    \draw[->] (4, 0) |- (7, 2);
    \draw[->] (-3, 0) |- (-4, 2);
    \draw[fill=black] (-3, 0) circle (1mm) node[below] {$-3$};
    \draw[fill=white] (-2, 0) circle (1mm) node[below] {$-2$};
    \draw[fill=black] (4, 0) circle (1mm) node[below] {$4$};
    \draw[fill=white] (6, 0) circle (1mm) node[below] {$6$};
\end{tikzpicture}
\end{document}

result

m0nhawk
  • 9,664