4

I have a display of two parallel lines on the Cartesian plane. I would like to have the region between them to be shaded to indicate that the display is that of the solution set to the inequality $\vert x + y - 2 \vert \leq 1$. I would like to see a display with gray shading using a \draw command with an option like fill=gray!25 and with hatching.

I have a bunch of dashed, gray horizontal and vertical lines drawn as a grid. The code I use to do this is ridiculous. How do I get these lines drawn using foreach?

\documentclass{amsart}
\usepackage{mathtools}

\usepackage{tikz}
\usetikzlibrary{calc}


\begin{document}

\begin{tikzpicture}

%Horizontal grid lines are drawn.
\draw[dashed,gray!50] (-3.25,-2.5) -- (3.25,-2.5);
\draw[dashed,gray!50] (-3.25,-2) -- (3.25,-2);
\draw[dashed,gray!50] (-3.25,-1.5) -- (3.25,-1.5);
\draw[dashed,gray!50] (-3.25,-1) -- (3.25,-1);
\draw[dashed,gray!50] (-3.25,-0.5) -- (3.25,-0.5);
\draw[dashed,gray!50] (-3.25,0) -- (3.25,0);
\draw[dashed,gray!50] (-3.25,0.5) -- (3.25,0.5);
\draw[dashed,gray!50] (-3.25,1) -- (3.25,1);
\draw[dashed,gray!50] (-3.25,1.5) -- (3.25,1.5);
\draw[dashed,gray!50] (-3.25,2) -- (3.25,2);
\draw[dashed,gray!50] (-3.25,2.5) -- (3.25,2.5);
\draw[dashed,gray!50] (-3.25,3) -- (3.25,3);
\draw[dashed,gray!50] (-3.25,3.5) -- (3.25,3.5);

%Vertical grid lines are drawn.
\draw[dashed,gray!50] (-3,-2.75) -- (-3,3.75);
\draw[dashed,gray!50] (-2.5,-2.75) -- (-2.5,3.75);
\draw[dashed,gray!50] (-2,-2.75) -- (-2,3.75);
\draw[dashed,gray!50] (-1.5,-2.75) -- (-1.5,3.75);
\draw[dashed,gray!50] (-1,-2.75) -- (-1,3.75);
\draw[dashed,gray!50] (-0.5,-2.75) -- (-0.5,3.75);
\draw[dashed,gray!50] (0,-2.75) -- (0,3.75);
\draw[dashed,gray!50] (0.5,-2.75) -- (0.5,3.75);
\draw[dashed,gray!50] (1,-2.75) -- (1,3.75);
\draw[dashed,gray!50] (1.5,-2.75) -- (1.5,3.75);
\draw[dashed,gray!50] (2,-2.75) -- (2,3.75);
\draw[dashed,gray!50] (2.5,-2.75) -- (2.5,3.75);
\draw[dashed,gray!50] (3,-2.75) -- (3,3.75);


%Some distances from the origin along the axes are labeled. (The horizontal spacing occupied by
%the minus sign indicating the additive inverse of a number is ignored so that the number is
%centered a horizontal or vertical line.)
\node[fill=white, anchor=north, inner sep=0.15cm, font=\tiny] at (-3,0){\makebox[0pt][r]{$-$}$6$};
\node[fill=white, anchor=north, inner sep=0.15cm, font=\tiny] at (-2,0){\makebox[0pt][r]{$-$}$4$};
\node[fill=white, anchor=north, inner sep=0.15cm, font=\tiny] at (-1,0){\makebox[0pt][r]{$-$}$2$};
\node[fill=white, anchor=north, inner sep=0.15cm, font=\tiny] at (1,0){2};
\node[fill=white, anchor=north, inner sep=0.15cm, font=\tiny] at (2,0){4};
\node[fill=white, anchor=north, inner sep=0.15cm, font=\tiny] at (3,0){6};

\node[fill=white, anchor=east, inner sep=0.15cm, font=\tiny] at (0,-2){\makebox[0pt][r]{$-$}$4$};
\node[fill=white, anchor=east, inner sep=0.15cm, font=\tiny] at (0,-1){\makebox[0pt][r]{$-$}$2$};
\node[fill=white, anchor=east, inner sep=0.15cm, font=\tiny] at (0,1){2};
\node[fill=white, anchor=east, inner sep=0.15cm, font=\tiny] at (0,2){4};
\node[fill=white, anchor=east, inner sep=0.15cm, font=\tiny] at (0,3){6};

%The axes are drawn.
\draw[latex-latex] (-3.5,0) -- (3.5,0);
\draw[latex-latex] (0,-3) -- (0,4);
\node [anchor=north west] at (3.5,0) {$x$};
\node [anchor=south west] at (0,4) {$y$};


%Line k is drawn.
\draw[<->] (-2.25,3.75) -- (3.25,-1.75);

%Line $\ell$ is drawn.
\draw[<->] (-3.25,3.75) -- (3.25,-2.75);

\end{tikzpicture}

\end{document}
6

Here's a modification of your original code, simplified using \foreach.

\documentclass{amsart}
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{calc,patterns}
\begin{document}
\begin{tikzpicture}[
  gridline/.style={dashed,gray!50},
  ticklabel/.style={fill=white, inner sep=0.15cm, font=\tiny}]

\foreach \x in {-2.5,-2,...,3.5} 
  \draw [gridline] (-3.25,\x) -- (3.25,\x);
\foreach \x in {-3,-2.5,...,3} 
  \draw [gridline] (\x,-2.75) -- (\x,3.75);

\foreach \x in {2,4,6} {
  \node [ticklabel,below] at (\x/2,0) {$\x$};
  \node [ticklabel,left] at (0,\x/2) {$\x$};
  \node [ticklabel,below] at (-\x/2,0) {\makebox[0pt][r]{$-$}$\x$};
}
\foreach \x in {2,4} 
  \node [ticklabel,left] at (0,-\x/2) {\makebox[0pt][r]{$-$}$\x$};

%The axes are drawn.
\draw[latex-latex] (-3.5,0) -- (3.5,0);
\draw[latex-latex] (0,-3) -- (0,4);
\node [anchor=north west] at (3.5,0) {$x$};
\node [anchor=south west] at (0,4) {$y$};


\fill[gray,opacity=0.25,postaction={pattern=north east lines}] (-2.25,3.75) coordinate (k1) -- (3.25,-1.75) coordinate (k2) --(3.25,-2.75) coordinate  (l2) -- (-3.25,3.75) coordinate (l1) -- cycle;

%Line k is drawn.
\draw[<->] (k1) -- (k2);

%Line $\ell$ is drawn.
\draw[<->] (l1) -- (l2);
\end{tikzpicture}
\end{document}

enter image description here

pgfplots

A version using pgfplots.

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{arrows.meta,patterns}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-6.5,xmax=6.5,ymin=-5.5,ymax=7.5,
axis lines=middle,
axis line style={Stealth-Stealth},
grid=both,
grid style={dashed,gray!50},
xlabel=$x$,ylabel=$y$,
domain=-10:10,samples=200,
restrict x to domain=-6.5:6.5,
restrict y to domain=-5.5:7.5,
axis equal,
minor tick num=1
]

\addplot [draw=none,name path=a] {-x+3};
\addplot [draw=none,name path=b] {-x+1};

\addplot [postaction={pattern=north east lines,opacity=0.25},fill=gray!25] fill between[of=a and b];

\addplot [<->] {-x+3};
\addplot [<->] {-x+1};
\end{axis}
\end{tikzpicture}
\end{document}
| improve this answer | |
  • Very nice! One edit in both graphs would make the display more appealing, I think. The shading obscures the arrowheads of the lines. Any suggestions? (Thanks for editing your response to include the \foreach command.) – Adelyn Apr 9 '16 at 16:07
  • @Adelyn For the first one it's simple: just switch the drawing order, do the fill before the drawing. Couldn't immediately think of an elegant way with pgfplots, so I just drew the lines twice. – Torbjørn T. Apr 9 '16 at 18:26
  • I posted code that gives shading that indicates that the solution set is of "infinite length." I gave the two "ends" of the solution set a jagged appearance but with some symmetry to still be aesthetic. I am fastidious about symmetry; so, the code is a bit messy. – Adelyn Apr 14 '16 at 14:50
0

With some help from Torbjørn T., I suggest this code.

\documentclass{amsart}
\usepackage{amsmath}
\usepackage{amsfonts}

\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections,quotes,decorations.markings,decorations.pathreplacing,backgrounds,patterns}



\begin{document}

\begin{tikzpicture}


%Some distances from the origin along the axes are labeled. (The horizontal spacing occupied by
%the minus sign indicating the additive inverse of a number is ignored so that the number is
%centered a horizontal or vertical line.)
\node[fill=white, anchor=north, inner sep=0.15cm, font=\tiny] at (-3,0){\makebox[0pt][r]{$-$}$6$};
\node[fill=white, anchor=north, inner sep=0.15cm, font=\tiny] at (-2,0){\makebox[0pt][r]{$-$}$4$};
\node[fill=white, anchor=north, inner sep=0.15cm, font=\tiny] at (-1,0){\makebox[0pt][r]{$-$}$2$};
\node[fill=white, anchor=north, inner sep=0.15cm, font=\tiny] at (1,0){2};
\node[fill=white, anchor=north, inner sep=0.15cm, font=\tiny] at (2,0){4};
\node[fill=white, anchor=north, inner sep=0.15cm, font=\tiny] at (3,0){6};

\node[fill=white, anchor=east, inner sep=0.15cm, font=\tiny] at (0,-2){\makebox[0pt][r]{$-$}$4$};
\node[fill=white, anchor=east, inner sep=0.15cm, font=\tiny] at (0,-1){\makebox[0pt][r]{$-$}$2$};
\node[fill=white, anchor=east, inner sep=0.15cm, font=\tiny] at (0,1){2};
\node[fill=white, anchor=east, inner sep=0.15cm, font=\tiny] at (0,2){4};
\node[fill=white, anchor=east, inner sep=0.15cm, font=\tiny] at (0,3){6};

%The axes are drawn.
\draw[latex-latex] (-3.5,0) -- (3.5,0);
\draw[latex-latex] (0,-3) -- (0,4);
\node [anchor=north west] at (3.5,0) {$x$};
\node [anchor=south west] at (0,4) {$y$};


%A grid on the Cartesian plane is drawn with dashed, gray lines.
\foreach \x in {-2.5,-2,...,3.5} \draw[dashed,gray!50] (-3.25,\x) -- (3.25,\x);
\foreach \x in {-3,-2.5,...,3} \draw[dashed,gray!50] (\x,-2.75) -- (\x,3.75);


%Line k is drawn. First, a thick, white line is drawn to separate the line from the
%shading.
\draw[draw=white, line width=1.2pt, <->] (-2.25,3.75) -- (3.25,-1.75);
\draw[<->] (-2.25,3.75) -- (3.25,-1.75);

%Line $\ell$ is drawn. First, a thick, white line is drawn to separate the line from the
%shading.
\draw[draw=white, line width=1.2pt, <->] (-3.25,3.75) -- (3.25,-2.75);
\draw[<->] (-3.25,3.75) -- (3.25,-2.75);


%A jagged boundary is determined by a polygonal line at an end of the
%region.
\coordinate (A_1) at (-2.15,3.65);
\coordinate (A_2) at ($(A_1) +(-110:0.25)$);
\coordinate (A_3) at (-2.75,3.75);
\path[name path=a_path_to_locate_A_4] (A_2) -- ($(A_2) +(-1,0)$);
\path let \p1=(A_2), \p2=(A_3), \n1={atan((\y1-\y2)/(\x1-\x2))}, \n2={veclen((\x2-\x1), (\y2-\y1))} in coordinate (A_4) at ($(A_3) +({-45+\n1}:\n2)$);
\path[name path=another_path_to_locate_A_4] (A_3) -- (A_4);
\path[name intersections={of=a_path_to_locate_A_4 and another_path_to_locate_A_4, by={A_4}}];
\coordinate (A_5) at (-3.15,3.65);

%A jagged boundary is determined by a polygonal line at another end of the
%region.
\coordinate (B_1) at (3.15,-1.65);
\coordinate (B_2) at ($(B_1) +(190:0.25)$);
\coordinate (B_3) at (3.25,-2.25);
\path[name path=a_path_to_locate_B_4] (B_2) -- ($(B_2) +(0,-1)$);
\path (B_2) -- (B_3);
\path let \p1=(B_2), \p2=(B_3), \n1={atan((\y1-\y2)/(\x1-\x2))}, \n2={veclen((\x2-\x1), (\y2-\y1))} in coordinate (B_4) at ($(B_3) +({(135-(\n1+180))+135}:\n2)$);
\path[name path=another_path_to_locate_B_4] (B_3) -- (B_4);
\path[name intersections={of=a_path_to_locate_B_4 and another_path_to_locate_B_4, by={B_4}}];
\coordinate (B_5) at (3.15,-2.65);

\draw[line width=0.2pt] (A_1) -- (A_2) -- (A_3) -- (A_4) -- (A_5);
\draw[line width=0.2pt] (B_1) -- (B_2) -- (B_3) -- (B_4) -- (B_5);

%The region between lines k and $\ell$ is shaded.
\fill[gray,opacity=0.25,postaction={pattern=north east lines}] (A_1) -- (A_2) -- (A_3) -- (A_4) -- (A_5) --
(B_5) -- (B_4) -- (B_3) -- (B_2) -- (B_1) -- cycle;


\end{tikzpicture}

\end{document}
| improve this answer | |
  • I'm just curious ... what is benefit or what you improved of your solution in comparison of @Torbjørn T. answer? Mix of TikZ code with \mbox is not good idea and repeating the same options in all draw commands is wast of code ... – Zarko Apr 14 '16 at 14:53
  • @Zarko See my last comment to Torbjørn T.. – Adelyn Apr 14 '16 at 14:57
  • @Zarko I understand that I repeat the options in \draw commands. I don't have "economical" code. – Adelyn Apr 14 '16 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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