# TikZ node text margins

I have nodes with text inside, however the nodes are too large for the text, with large margins.

I want to keep the text size the same, but make the box tighter. I’ve tried varying text height and width, as well as inner seps, however these only move the text around.

The code below produces the following output:

documentclass[tikz, border=2mm]{standalone}

usetikzlibrary{positioning,shapes,arrows,backgrounds,external,fit,calc}
usepackage[T1]{fontenc}
usepackage[utf8]{inputenc}
usepackage{lmodern}
usepackage{helvet}
renewcommand{familydefault}{sfdefault}

definecolor{CoreBlue}{HTML}{5b9bd5}

tikzset{
Core/.style={rectangle, draw, fill=CoreBlue,  draw opacity=0, text=white},
}

begin{document}
begin{tikzpicture}[font={sffamilyscriptsize}]
node[Core,minimum width=12mm,minimum height=8mm, anchor=west,
% inner sep=-10mm,  % no change
% inner ysep=-10mm,  % no change
% text height=1mm,  % changes text position, but not node size
% text width=0.2cm,  % changes text position, but not node size
] (1) at (0,0) {scriptsize Lorem};
end{tikzpicture}
end{document}


However, I am looking for something more like:

TeX - LaTeX Asked on November 21, 2021

This is not an answer but a long comment. If OP just want colored boxes for short texts, an alternative to TikZ could be tcolorbox. It's easier to decide margins because they (top, bottom, left and right) are independent, it's easier to decide alignment, ...

documentclass{article}

usepackage[T1]{fontenc}
usepackage[utf8]{inputenc}
usepackage{lmodern}
usepackage{helvet}
renewcommand{familydefault}{sfdefault}

usepackage[most]{tcolorbox}

definecolor{CoreBlue}{HTML}{5b9bd5}

newtcbox{mybox}[1][]{%
enhanced, colback=CoreBlue, fontupper=sffamilyscriptsize,
sharp corners, frame hidden, colupper=white, #1}

begin{document}
mybox{Lorem}

mybox[boxsep=0pt]{Lorem}

mybox[left=5mm, right=1mm, top=1mm, bottom=0pt]{Lorem}

mybox[size=small]{Lorem}

mybox[size=tight]{Lorem}
end{document}


Answered by Ignasi on November 21, 2021

• In accepted answer the size of nodes is set by
base/.default = 17mm


This size you can locally change, for example as

node (1a) [CB=13mm] {Lorem};

• However, if in your node text has only one line, than you should redefine the style Core as follows:
    Core/.style={fill=CoreBlue},


In this case distances between text in node and node borders is equal to default value of inner sep. If you not like this distance size, you can change it with style parameter inner sep, for example as:

    Core/.style={fill=CoreBlue, inner sep=3mm},


A complete MWE using style names from my answer is:

documentclass[tikz, border=2mm]{standalone}
definecolor{CoreBlue}{HTML}{5b9bd5}
definecolor{CoreOrange}{HTML}{ec7d2d}

begin{document}
begin{tikzpicture}[
CB/.style = {fill=CoreBlue, inner sep=2mm}
]
node (1)  [CB] {Lorem};
end{tikzpicture}
end{document}


Note: it is not entirely clear what is your problem. You should provide more context of using your nodes and how they should be designed.

Answered by Zarko on November 21, 2021

You are setting minimum width=12mm and minimum height=8mm so your box is going to be at least this big and you will have "large margins" unless your text can fill the void. If you do not want the margins then take out the minimum width and height specifications. If, in addition, you add inner sep=0.5mm then you get pretty close to what you are asking for:

(I've zoomed in on the image, which is why it is slightly bigger than above.) Now that the width and height are not fixed, tikz is able to adjust the size of the box so that it fits the text.

Here's the updated code:

documentclass[tikz, border=2mm]{standalone}

usetikzlibrary{positioning,shapes,arrows,backgrounds,external,fit,calc}
usepackage[T1]{fontenc}
usepackage[utf8]{inputenc}
usepackage{lmodern}
usepackage{helvet}
renewcommand{familydefault}{sfdefault}

definecolor{CoreBlue}{HTML}{5b9bd5}

tikzset{
Core/.style={rectangle, draw, fill=CoreBlue,  draw opacity=0, text=white},
}

begin{document}
begin{tikzpicture}[font={sffamilyscriptsize}]
node[Core,inner sep=0.5mm,anchor=west,
] (1) at (0,0) {scriptsize Lorem};
end{tikzpicture}
end{document}
`

Answered by user30471 on November 21, 2021

## Related Questions

### How to get what is before the next ‘[‘ or ‘]’

4  Asked on January 29, 2021 by jrme-laurens

### Create a math quiz with LaTex by randomly choosing questions from a bank

0  Asked on January 29, 2021

### Why are pipe characters horizontal when using bxcjkjatype?

1  Asked on January 29, 2021 by polm23

2  Asked on January 29, 2021 by casimir

### Missing delimiter (. inserted). That mistake showed up when I try to compile the code below, What’s wrong with it?

1  Asked on January 28, 2021

### Is there any way to recover the text file or at least the PDF file from corrupt text file in LaTeX?

0  Asked on January 28, 2021 by m-a-sarkar

### Using listings linerange in tcolorbox breaks formatting

1  Asked on January 28, 2021 by takingitcasual

### Macro – Introduce a constant to differ the index of the right-hand side image, compared to the left-hand side image

0  Asked on January 28, 2021 by o0123

### Image path when exporting from Lyx

1  Asked on January 28, 2021 by muzzle

### How to produce bibs of the style used in the figure

0  Asked on January 28, 2021

### Centralized bibliography, build/auxiliary folder, and making sure autocomplete still works

1  Asked on January 28, 2021 by artificial_moonlet

0  Asked on January 28, 2021

### table with siunitx – alignment on precision mode

1  Asked on January 28, 2021

### Compiler says that hbar is invalid in math mode, but I can’t figure out what I should be using instead — all the sources say hbar?

0  Asked on January 28, 2021 by sophibug

### diff against the working directory with git-latexdiff

2  Asked on January 28, 2021 by user2740

### How to disable newpage

1  Asked on January 28, 2021

### How to use title variable inside a class?

1  Asked on January 28, 2021

### Defining -aux-directory for pdflatex breaks citations for natbib

1  Asked on January 28, 2021 by roboreb

### How to prevent latex from removing the margin from a PDF image

2  Asked on January 28, 2021

### TikZ Graph with usebox commands not working

1  Asked on January 28, 2021