4

I would like to write an a in the rectangle that I marked below. I suppose the solution uses nodes somehow but until now I could not figure it out. Thanks in advance!

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}[scale=.35]\footnotesize
 \pgfmathsetmacro{\xone}{0}
 \pgfmathsetmacro{\xtwo}{20}
 \pgfmathsetmacro{\yone}{0}
 \pgfmathsetmacro{\ytwo}{5}

\begin{scope}<+->;
% grid
  \draw[step=1cm,gray,very thin] (\xone,\yone) grid (\xtwo,\ytwo);

% ticks
  \foreach \x/\xtext in { 5/q_1, 6/q_2, 7/q_3, 8/q_4, 9/q_5, 10/q_6} % xKoordinate / Beschriftung
  \draw[gray,xshift=\x cm] (0,-.3) -- (0,0) node[below] {$\xtext$};

\end{scope}

% function
\begin{scope}[thick,red]
\filldraw[thin,red,opacity=.3] (0,0) rectangle (6,1);
\filldraw[thin,red,opacity=.3] (0,1) rectangle (10,2);
\filldraw[thin,blue,opacity=.3] (0,2) rectangle (3,3);
\filldraw[thin,blue,opacity=.3] (0,3) rectangle (5,4);
\filldraw[thin,blue,opacity=.3] (0,4) rectangle (4,5);
\draw[thick, black] (5,0) -- (5,5);
\draw [decorate,decoration={brace,amplitude=5pt},xshift=-4pt,yshift=0pt]
(0,2) -- (0,5) node [black,midway,xshift=-0.6cm] 
{\footnotesize $\bar{x}$};
\draw [decorate,decoration={brace,amplitude=5pt},xshift=-4pt,yshift=0pt] %Amplitude für wie weit nach rechts gezogen
(0,0) -- (0,2) node [black,midway,xshift=-0.6cm] 
{\footnotesize $\bar{y}$};


\end{scope}
\end{tikzpicture}
\end{document} 

1 Answer 1

4

As you have created a grid with steps of size 1, a "quick and dirty" solution is to locate a node in the exact coordinates that you want the text, in this case (7.5,1.5) if I counted well.

The only thing that you need to add to your code is:

\begin{scope}[color=black]
    \node[anchor = center] () at (7.5,1.5){a};
\end{scope}

at the end. I located it on a scope just inc ase you want to change the color, but it is not neccesary as black is the default color, as far as I know. The code will end us as:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}[scale=.35]\footnotesize
 \pgfmathsetmacro{\xone}{0}
 \pgfmathsetmacro{\xtwo}{20}
 \pgfmathsetmacro{\yone}{0}
 \pgfmathsetmacro{\ytwo}{5}

\begin{scope}<+->;
% grid
  \draw[step=1cm,gray,very thin] (\xone,\yone) grid (\xtwo,\ytwo);

% ticks
  \foreach \x/\xtext in { 5/q_1, 6/q_2, 7/q_3, 8/q_4, 9/q_5, 10/q_6} % xKoordinate / Beschriftung
  \draw[gray,xshift=\x cm] (0,-.3) -- (0,0) node[below] {$\xtext$};

\end{scope}

% function
\begin{scope}[thick,red]
\filldraw[thin,red,opacity=.3] (0,0) rectangle (6,1);
\filldraw[thin,red,opacity=.3] (0,1) rectangle (10,2);
\filldraw[thin,blue,opacity=.3] (0,2) rectangle (3,3);
\filldraw[thin,blue,opacity=.3] (0,3) rectangle (5,4);
\filldraw[thin,blue,opacity=.3] (0,4) rectangle (4,5);
\draw[thick, black] (5,0) -- (5,5);
\draw [decorate,decoration={brace,amplitude=5pt},xshift=-4pt,yshift=0pt]
(0,2) -- (0,5) node [black,midway,xshift=-0.6cm] 
{\footnotesize $\bar{x}$};
\draw [decorate,decoration={brace,amplitude=5pt},xshift=-4pt,yshift=0pt] %Amplitude für wie weit nach rechts gezogen
(0,0) -- (0,2) node [black,midway,xshift=-0.6cm] 
{\footnotesize $\bar{y}$};
\node () at (7.5,1.5){a};
\end{scope}

%- Node!
\begin{scope}[color=black]
    \node[anchor = center] () at (7.5,1.5){a};
\end{scope}

\end{tikzpicture}
\end{document} 

enter image description here

EDIT: I did not count well, as the "a" is shifted from where it should be. Another solution is to add concrete names to your ticks, add a node between them and yshift it (this last part needs rework, I am not sure how to do it). A starting point, changing the following lines:

1- Use calc library \usetikzlibrary{decorations.pathreplacing,calc}

2- "Names" each node so afterwards you can use its coordinates

  \foreach \x/\xtext in { 5/q_1, 6/q_2, 7/q_3, 8/q_4, 9/q_5, 10/q_6} % xKoordinate / Beschriftung
  \draw[gray,xshift=\x cm] (0,-.3) -- (0,0) node[below] (\xtext) {$\xtext$};

3- Places a node between q2 and q3 and (manually D: ) shifts it until desired position in Y axis

\begin{scope}[color=black]
    \node[yshift=0.7cm] () at ($(q_2)!0.5!(q_3)$) {a};
\end{scope}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .