繪製錯誤的矩形

繪製錯誤的矩形

我可能不明白使用 TikZ 繪製矩形。下面的程式碼顯示了清晰的繪圖,但即使它們\g相同\w,輸出檔案也是錯誤的。

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[czech]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\def \a {3}
\def \w {0.5}
\def \g {0.5}
\def \s14 {0.5}
\def \s34 {0.5}

\filldraw [blue] (0,0) rectangle (\a, \a);
\filldraw [white] (\w,\w) rectangle (\a - \w,\a - \w);
\filldraw [yellow] (0,\a/2 + \g/2) rectangle (\w,\g);
\end{tikzpicture}

\end{document}

在此輸入影像描述

我對輸出繪圖做了一些註解。黃色區域是不需要的,gxw 矩形的位置和大小清晰顯示。你能幫我嗎?

答案1

你想要這個嗎?你可以用來+告訴TikZ 表示第二個座標相對於第一個座標。

\documentclass[10pt,a4paper]{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\newcommand* \mylengthA {3}
\newcommand* \mylengthW {0.5}
\newcommand* \mylengthG {0.5}


\fill [blue] (0,0) rectangle (\mylengthA, \mylengthA);
\fill [white]
  (\mylengthW,\mylengthW) rectangle
  (\mylengthA - \mylengthW,\mylengthA - \mylengthW);
\fill [yellow]
  (0,\mylengthA/2 + \mylengthG/2) rectangle +(\mylengthW,-\mylengthG);
\end{tikzpicture}

\end{document}

在此輸入影像描述

rectangle在給定的兩個座標之間繪製一個矩形。如果沒有,+它們被認為是絕對的並且彼此不相關。所以\draw (1,1) rectangle (2,2);會在這兩點之間畫一個邊長為 1 公分的長方形。

相關內容