# Diagram with a shaded region

The following MWE yields the diagram above but I would like to find a better way of showing the shaded region.

\documentclass[letterpaper]{article}
\usepackage{tikz}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}
\draw[fill=black!20!white] (0,2)--(2,0)--(3,0)--(3,2)--cycle;
\draw (0,0) --node[below] {$2x$}(2,0)--node[below] {$\vphantom{2}x$}(3,0)--(3,2)--(0,2)-- node[left] {$x-7$}cycle;
\end{tikzpicture}
\end{document}

Note that the top left corner does not look pretty. Am thinking about clipping but seems a bit too much.

You can exploit the path picture key of the nodes:

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{calc}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}
\draw[fill=black!20!white] (0,2)--(2,0)--(3,0)--(3,2)--cycle;
\draw (0,0) --node[below] {$2x$}(2,0)--node[below] {$\vphantom{2}x$}(3,0)--(3,2)--(0,2)-- node[left] {$x-7$}cycle;
\end{tikzpicture}

\begin{tikzpicture}
\node[rectangle, minimum width=3cm, minimum height=2cm, inner sep=0pt, fill=white,draw, path picture={%
\fill[black!20]($(path picture bounding box.south)!.35!(path picture bounding box.south east)$) coordinate(a)--
(path picture bounding box.north west)--
(path picture bounding box.north east)|-
(path picture bounding box.south); % filling
\draw (a)--(path picture bounding box.north west);% line
}] (x) at (0,0){};
\node[left=2pt] at (x.west){$x-7$};
\node[below] at ($(x.south)!.35!(x.south west)$){$2x$};
\node[below] at ($(x.south)!.65!(x.south east)$){$\vphantom{2}x$};
\end{tikzpicture}
\end{document}

The result:

• Quite interesting! Thought clipping was the only way although I didn't do it that way. – azetina Apr 25 '16 at 21:49
• @azetina: of course, but clipping is too simple and not much esoteric ;) – Claudio Fiandrino Apr 25 '16 at 21:51

A PSTricks solution:

\documentclass{article}

\psset{linejoin = 1}

\def\Label[#1](#2)(#3)#4{%
\pcline[linestyle = none, offset = #1](#2)(#3)
\ncput{#4}}

% parameters
\def\width{6}
\def\height{4}

\begin{document}

\begin{pspicture}(-0.93,-0.4)(\width,\height)
\pnodes{P}(0,0)(0,\height)(\width,\height)(\width,0)(!2 3 div \width\space mul 0)
\pspolygon(P0)(P1)(P4)
\pspolygon[
fillstyle = solid,
fillcolor = gray!50
](P1)(P2)(P3)(P4)
\Label[15pt](P0)(P1){$x - 7$}
\Label[-7pt](P0)(P4){$2x$}
\Label[-7pt](P4)(P3){$x$}
\end{pspicture}

\end{document}

All you have to do is change the values of the parameters and the drawing will by adjusted accordingly.

• But you have the same problem with the top left corner as the OP – JMP Apr 26 '16 at 20:47
• @JMP Just corrected; I noticed it immediately after I posted my answer. (I missed \psset{linejoin = 1}.) – Svend Tveskæg Apr 26 '16 at 20:48

For comparison with Metapost and luamplib. Compile with lualatex.

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
u = 1mm;
x = 20u;

box = unitsquare xscaled 3x yscaled 2x;
shade = subpath (2/3,3) of box -- cycle;

draw box;

label.bot("$2x$",  point 1/3 of box);
label.bot("$x$",   point 5/6 of box);
label.lft("$x-7$", point 7/2 of box);

endfig;
\end{mplibcode}
\end{document}

## Notes

• The built-in path unitsquare has four "points" corresponding to the the four corners and numbered clockwise from zero starting at the bottom left. Hence point 2/3 of box is 2/3 of the way along the bottom edge, and point 7/2 of box is halfway down the left hand edge.

• subpath (x,y) of path runs from point x of path to point y of path.

• It's tempting to "correct" the diagram to draw it to scale. If you replace the line defining the box with

box = unitsquare xscaled 3x yscaled (x-7u);

then you get this slightly more believable figure.