2

I'm still a novice in tikz, and I would like to ask how to draw and fill the rectangle, as in the following image:

enter image description here

Here's my very simple MWE

\documentclass[12pt,a4paper]{book}


\usepackage{tikz}
\usetikzlibrary{calc,shadings} 
\usepackage{pgf,tikz,pgfplots}
\usetikzlibrary{arrows}


\begin{document}

\begin{tikzpicture}[dot/.style={circle,inner sep=1pt,fill,label={#1},name=#1},
  extended line/.style={shorten >=-#1,shorten <=-#1},
  extended line/.default=1cm]

\coordinate (A) at (0,2);
\coordinate (B) at (4.5,0);
\coordinate (C) at (2,4);
\coordinate (D) at (4,-1.4);

\draw (A)--(B) node[anchor=north west] {\small $p'x=\alpha$} coordinate[midway] (M) coordinate[pos=0.85](k);
\draw[-latex] ($(M)!0cm!270:(B)$)--($(M)!2cm!90:(B)$) node[anchor=north west]{$p$};

\shade[upper right=gray!,lower left=gray!, fill opacity=0.3] (3,3) rectangle (M);

\node[dot=below left:$x_{0}$] at (M) {};
\draw[-latex] (M)--(C) node[pos=0.95, anchor=north east] {\small $x_{1}$};
\draw[-latex] (M)--(D) node[pos=0.95, anchor=north east] {\small $x_{2}$};

\end{tikzpicture}


\end{document}

Is there a better and faster way to draw this, maybe by way of "path"?

  • 1
    Most commands are actually \path commands: \draw is \path[draw], \fill is \path[fill], \node is \path node, etc. – John Kormylo Jun 12 at 16:01
3

Your code does what you want. I've only included the dot (M) into the command which draws the arrow from M and a possible code to draw the gray rectangle:

\documentclass[12pt,a4paper]{book}


\usepackage{tikz}
\usetikzlibrary{calc,shadings} 
\usepackage{pgf,tikz,pgfplots}
\usetikzlibrary{arrows}


\begin{document}

\begin{tikzpicture}[dot/.style={circle,inner sep=1pt,fill,label={#1},name=#1},
  extended line/.style={shorten >=-#1,shorten <=-#1},
  extended line/.default=1cm]

\coordinate (A) at (0,2);
\coordinate (B) at (4.5,0);
\coordinate (C) at (2,4);
\coordinate (D) at (4,-1.4);

\draw (A)--(B) node[anchor=north west] {\small $p'x=\alpha$} coordinate[midway] (M) coordinate[pos=0.85](k);
\draw[-latex] (M) node[dot=below left:$x_{0}$] at (M) {} --($(M)!2cm!90:(B)$) node[anchor=north west]{$p$};

\fill[gray!30] (A) -- ($(A)!2cm!-90:(B)$) -- ($(B)!2cm!90:(A)$) -- (B)--cycle;

%\shade[upper right=gray!,lower left=gray!, fill opacity=0.3] (3,3) rectangle (M);

%\node[dot=below left:$x_{0}$] at (M) {};
\draw[-latex] (M)--(C) node[pos=0.95, anchor=north east] {\small $x_{1}$};
\draw[-latex] (M)--(D) node[pos=0.95, anchor=north east] {\small $x_{2}$};

\end{tikzpicture}


\end{document}

enter image description here

  • thank you! I tried to add node[pos=1, anchor=north west]{$p'x\leq \alpha$} after the \fill command to insert a label, but it doesn't show up, as if it was hidden under the rectangle, maybe this is related to opacity? – Nenne Jun 12 at 16:31
  • 1
    @Nenne If you added the node into the fill command, the contents is written in gray and you won't see it. Change the color to black. – Ignasi Jun 12 at 19:19

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.