9

I have below code section, the label "B" is displayed at the outside of the corner, how can we swap it to the inner corner? or any other good suggestion to label it?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,calc}
\begin{document}

\begin{tikzpicture}[inner sep=0mm,outer sep=0,node distance=4em,
  box/.style={draw,anchor=west,minimum width=10em,minimum height=3em},
]
  \node (A) [box] {A};
  \node (B) [below=of A.west,box,label=south west:B] {};  
\end{tikzpicture}
\end{document}

The output picture is:

enter image description here

  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. And please do us a favour and change your username to something more telling than “user1234”. – Tobi Jan 4 '14 at 0:05
5

A label is supposed to exist outside a node so as to avoid confusion between the contents and label. The easiest IMO is to add an extra node for labelling.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,calc}
\begin{document}

\begin{tikzpicture}[inner sep=0mm,outer sep=0,node distance=4em,
  box/.style={draw,anchor=west,minimum width=10em,minimum height=3em},
]
  \node (A) [box] {A};
  \node (B) [below=of A.west,box] {}node at ($(B.south west) +(0.15,0.15)$) {B};
\end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |
6

An alternative to @Harish's solution (which I think is easier) is to manually set the anchor of the label node. I follow this answer and define an inside key, which causes labels to be put "inside" a node:

\usepackage{etoolbox}
\makeatletter
\tikzset{inside/.code=\preto\tikz@auto@anchor{\pgf@x-\pgf@x\pgf@y-\pgf@y}}
\makeatother

Then one can pass this key to the label operation along with other options (e.g. inner sep=1pt to give extra padding to the label node).

Code

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,calc}
\usepackage{etoolbox}
\makeatletter
\tikzset{inside/.code=\preto\tikz@auto@anchor{\pgf@x-\pgf@x\pgf@y-\pgf@y}}
\makeatother

\begin{document}

\begin{tikzpicture}[inner sep=0mm,outer sep=0,node distance=4em,
  box/.style={draw,anchor=west,minimum width=10em,minimum height=3em},
]
  \node (A) [box] {A};
  \node (B) [below=of A.west,box,label={[inside,inner sep=1pt]south west:B}] {};  
\end{tikzpicture}
\end{document}

Output

enter image description here

| improve this answer | |
5

This attempt is also an alternative solution when compared with @Harish and @Kevin's solutions (which I think is easy and simple). The first one need calc, the other needs inside/.code. This solution uses anchor and at of node position commands.

enter image description here

Code

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,calc}
\begin{document}

\begin{tikzpicture}[inner sep=0.1mm,outer sep=0,node distance=4em,
  box/.style={draw,anchor=west,minimum width=10em,minimum height=3em},
]
  \node (A) [box] {A};
  \node (B) [below=of A.west, box] {}; 
  \node     [anchor=south west, at=(B.south west)]{B};
\end{tikzpicture}
\end{document}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.