我对 tikz 还是新手,想问一下如何绘制并填充矩形,如下图所示:
以下是我非常简单的 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}
有没有更好更快的方法来绘制它,也许通过“路径”?
答案1
您的代码实现了您想要的功能。我仅将dot
(M) 包含在绘制箭头的命令中M
,以及绘制灰色矩形的可能代码:
\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}