How to make a label in a square in TikZ?

I don't know exactly what you mean with slashes but here is my attempt.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{patterns}

\begin{document}

\begin{tikzpicture}[scale=0.5]
  \draw (0,0) -- (12,0) -- (0,5) -- cycle;
  \filldraw[pattern=north east lines] (0,0) rectangle (60/17,60/17) node[pos=0.5] {$S_1$};
\end{tikzpicture}
\end{document}

enter image description here


One of the major strengths of TikZ is that one can create any number of nodes half way on a path. We can exploit that feature and create the square on the same triangular path. Also, a white background makes the label clearer.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns}

\begin{document}

\tikzset{sq/.style={draw,anchor=south west,minimum size=30cm/17,pattern=north east lines,outer sep=0pt}}

\begin{tikzpicture}[scale=0.5]
  \draw (0,0) node[sq]{\colorbox{white}{$S_1$}} -- (12,0) -- (0,5) -- cycle;
\end{tikzpicture}

\end{document}

enter image description here

Tags:

Tikz Pgf