0

I want the label of a node to be included in the node, I use for that label = {[below]: label} but if I do that, the text of the node is written above the label. How to make the contents of the node start only under the label.

\documentclass{article}

\usepackage{tikz}

\begin{document}

 \begin{tikzpicture}
 \node[text width=6em,draw,label={[below,draw]:label}] 
 { un texte un peu long sur plusieurs lignes};
 \end{tikzpicture}

\end{document}
2

if you need to use TikZ you can do a command (or pic). Two different nodes inside a fit one.

\documentclass{article}

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

\newcommand{\mynode}[2]{%
 \node[text width=6em, outer sep=0pt] (a) 
 {#2};
 \node[draw, above=0pt of a, outer sep=0pt] (b) 
 {#1};
 \node[fit=(a) (b), draw, inner sep=0pt]{};
}

\begin{document}

 \begin{tikzpicture}
 \node[text width=6em,draw,label={[below,draw]:label}] 
 { un texte un peu long sur plusieurs lignes};
 \end{tikzpicture}

 \begin{tikzpicture}
 \mynode{label}{ un texte un peu long sur plusieurs lignes}
 \end{tikzpicture}

\end{document}

enter image description here

If you can accept tcolorbox a solution could be:

\documentclass{article}

\usepackage[most]{tcolorbox}

\newtcolorbox{mybox}[2][]{
    title=#2,
    enhanced,
    sharp corners,
    size=small,
    colback=white,
    attach boxed title to top center={yshift*=-\tcboxedtitleheight},
    text width=6em,
    halign=flush left,
    coltitle=black,
    boxed title style={colback=white, sharp corners, size=small},
    #1
    }

\begin{document}
\begin{mybox}{label}
un texte un peu long sur plusieurs lignes
\end{mybox}
\end{document}

enter image description here

EDIT: rpapa reminded me my own answer to TikZ: How to define a node with a custom inner sep on one side (e.g. on the right side of an rectangle)?. Adapting it to the new scheme, it's possible to do something like:

\documentclass{article}

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

\tikzset{
    withlabel/.style={%
        append after command={%
            \pgfextra
                    \node[draw, anchor=south, outer sep=0pt] (\tikzlastnode-label) at (\tikzlastnode.north) {#1};
                \node[fit={(\tikzlastnode) (\tikzlastnode-label)}, 
                draw, inner sep=0pt]  (\tikzlastnode) {};\endpgfextra}}
    }
\begin{document}
    \begin{tikzpicture}
    \node[text width=6em, withlabel={label a}] (a) {some long text which uses more than one line};

    \node[text width=8em, withlabel={label b}, right=of a] (b) {some long text which uses more than one line};

    \node[text width=10em, withlabel={label c}, below left=2cm and 1cm of b] (c) {some long text which uses more than one line};

    \end{tikzpicture}
\end{document}

enter image description here

8
  • it's already what I'm doing, but I'd prefer to keep the functioning of a node. I want to be able to write\node[mynode,label={xxx}]{texte};
    – rpapa
    Mar 15 '18 at 11:49
  • can we modify inner sep so that the larger difference is larger
    – rpapa
    Mar 15 '18 at 12:21
  • @rpapa yes you can adjust inner sep to a larger value, but it's applied to four margins (inner sep) or to two margins (inner {x|y}sep). Therefore at least bottom margin will be as large as upper. Another solution could be to insert a vertical space before node's text.
    – Ignasi
    Mar 15 '18 at 12:50
  • only for the top, I have seen this answer tex.stackexchange.com/questions/376065/…, but I do not see how to use it
    – rpapa
    Mar 15 '18 at 12:58
  • @rpapa with the update you can use a node syntax. Hope it's better now.
    – Ignasi
    Mar 15 '18 at 14:32
1

An alternative to Ignasi's nice answer would be to simply make the node a bit larger with minimum height and shift the text by hand.

\documentclass{article}

\usepackage{tikz}

\begin{document}

 \begin{tikzpicture}
 \node[text width=6em,draw,label={[below,draw]:label},minimum height=2.5cm,
 text depth=0.5cm] 
 { un texte un peu long sur plusieurs lignes};
 \end{tikzpicture}

\end{document}

enter image description here

This approach comes with a shorter code but you need to adjust minimum height and text depth by hand.

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.