1

The label of a fit can be placed relatively to it like:

    \documentclass[border=1pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{automata,positioning,fit,backgrounds}

\tikzstyle{back}=[rectangle,
fill=blue!30,
inner sep=0.2cm,
rounded corners=3mm]

\begin{document}

    \begin{tikzpicture}[shorten >=1pt,node distance=1cm,on grid,auto] 
    \node[] (1) {\textbf{1}}; 
    \node[] (2) [right= of 1]  {\textbf{2}}; 

    \begin{pgfonlayer}{background}
    \node [back,
    fit=(1) (2),
    label=above:{\tiny Introduction}] {};
    \end{pgfonlayer}
    \end{tikzpicture}

\end{document}  

But what if I want to shift that label, for example, to the right? How should I use \xshift in this case?

2 Answers2

2

It is possible to specify options for a label in square brackets, see TikZ documentation page 239f. Because you have not provided a complete example I have based this answer on an example from the TikZ documentation page 240:

\documentclass[tikz]{standalone}
\begin{document}
\tikz \node [rectangle,draw,label={[red, xshift=1em]center:R}] {main node};
\end{document}

enter image description here

jakun
  • 5,981
  • 1
    I think it doesn't work in the case of the OP's intention. –  Nov 04 '17 at 06:57
  • label={[xshift=2mm]above}:{\tiny Introduction}] {}; doesn't work. –  Nov 04 '17 at 07:03
  • Please don't downvote since the answerer may fix the problem. –  Nov 04 '17 at 07:06
  • 2
    @A.Loc label={[xshift=2mm]above:{\tiny Introduction}}] works for me, you forgot a } after Introduction. – CarLaTeX Nov 04 '17 at 07:08
  • 2
    @A.Loc As CarLaTeX says, pay attention to the position of the braces. You want braces around the entire label construct, options, position and the text itself, i.e. label={ ... }, not label={..}:{..}. So the closing brace before the : is wrong, you want just {[xshift=2mm]above:\tiny Introduction}, which is the same construct as in jakun's answer. – Torbjørn T. Nov 04 '17 at 10:01
1

This works for me.

See also Should \tikzset or \tikzstyle be used to define TikZ styles?.

\documentclass[border=1pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{automata,positioning,fit,backgrounds}
\tikzset{
    back/.style={rectangle,
    fill=blue!30,
    inner sep=0.2cm,
    rounded corners=3mm},
    }

\begin{document}

    \begin{tikzpicture}[shorten >=1pt,node distance=1cm,on grid,auto] 
    \node (1) {\textbf{1}}; 
    \node (2) [right= of 1] {\textbf{2}}; 

    \begin{pgfonlayer}{background}
        \node [back,
            fit=(1) (2),
            label={[xshift=2mm]above:{\tiny Introduction}}] {};
    \end{pgfonlayer}
    \end{tikzpicture}

\end{document}  

enter image description here

CarLaTeX
  • 62,716