tikz 中的矩形

tikz 中的矩形

你能帮助我吗?为了获得这个: 在此处输入图片描述

我尝试这样做:

\documentclass[tikz,border=7mm]{standalone}
\begin{document}
\begin{tikzpicture}[scale=3]
    \draw (0,0) rectangle
          (2,1) coordinate(C) node[above]{} -- (0,0) coordinate(A) node[below]              {}
          (0,1) coordinate(D) node[,above]{} -- (2,0) coordinate(B) node[below]{y};
 \path ($(B)!0.5!(D)$)--(D)node[midway,below,right=0.5mm]{$x-y$};
  \path ($(A)!0.5!(C)$)--(C)node[midway,below]{$x+y$};   
  \path  ($(A)!0.5!(D)$)--(A)node[midway,left=0.5mm]{$x$};
  \end{tikzpicture}
\end{document}

请问如何得到虚线?

答案1

输出

\documentclass[tikz,border=10pt]{standalone}
\begin{document}
  \begin{tikzpicture}[scale=2]
    % Dashed part (top left to bottom right)
    \draw[dashed]  node[below]{\small $y$}(0,0)--(0,1)--(-2,1);
    % Solid part (top right to bottom left)
    \draw[solid] (-2,1)--node[pos=0.5, left]{\small $x$}(-2,0)--(0,0);
    % Diagonal (top left to bottom right)
    \draw[solid] (-2,1)--(0,0) node[pos=0.2,rotate=-25, below]{\small $x-y$};
    % Diagonal (top right to bottom left)
    \draw[solid] (-2,0)--(0,1) node[pos=0.8,rotate=25, below]{\small $x+y$};
  \end{tikzpicture}
\end{document}

答案2

请尝试以下操作:

\documentclass[tikz,border=7mm]{standalone}
\usetikzlibrary{quotes}

\begin{document}
\begin{tikzpicture}[scale=3, 
every edge quotes/.style = {auto, inner sep=2pt, font=\footnotesize, 
                           pos=0.7, sloped}
                    ]
\draw   (0,0) -- (0,1)  node[above] {$x$}  
              -- (2,0)  node[below] {$y$} -- (0,0)
              edge ["$x+y$"]        (2,1)
        (2,0) edge ["$x-y$"]        (0,1);
\draw[densely dashed]
        (0,1) -| (2,.0);
\end{tikzpicture}
\end{document}

在此处输入图片描述

从您的草图中无法清楚看出这X是左上角的名称还是左矩形边框的名称。如果情况较晚,则在tikzpicture代码中使用以下更改:

\begin{tikzpicture}[scale=3, 
every edge quotes/.style = {auto, inner sep=2pt, font=\footnotesize,sloped}
                    ]
\draw   (0,0) edge ["$x$",pos=0.5] (0,1)
              -- (2,0) node[below] {$y$} -- (0,0)
              edge ["$x+y$",pos=0.7]        (2,1)
        (2,0) edge ["$x-y$",pos=0.7]        (0,1);
\draw[densely dashed]
        (0,1) -| (2,.0);
\end{tikzpicture}

在此处输入图片描述

答案3

在此处输入图片描述

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[line join=round]
\def\x{2.5} \def\y{5}
\draw[dashed] (\y,\x)--(0,\x) (\y,\x)--(\y,0);
\draw (\y,\x)
--(0,0)  node[sloped,below,pos=.3]{$x+y$}
--(\y,0) node[below]{$y$}
--(0,\x) node[sloped,below,pos=.7]{$x-y$} node[left]{$x$}
--(0,0);
\end{tikzpicture}
\end{document}

相关内容