# How can I make custom section label inline with name?

So I decided to try TikZ With titlesec by customising the section header design. This is what I have so far.

\documentclass[10pt]{article}

\usepackage{tikz}
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage[
a4paper,
twoside,
portrait,
top=1.6cm,
bottom=1.6cm,
left=0.75in,
right=0.75in,
footskip=1.4cm, % Space from the bottom margin to the baseline of the footer
headsep=10pt, % Space from the top margin to the baseline of the header
heightrounded
]{geometry}
\usepackage{blindtext}

\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{tcolorbox}
\setmainfont[
BoldFont = Roboto-Bold
]{Roboto}
\defaultfontfeatures{Ligatures=TeX}
\defaultfontfeatures{Scale = MatchLowercase}

\titleformat{\section}
{\Large\bfseries\color{black}}
{
\begin{tikzpicture}
\node (rect) at (0pt,0pt) [draw,thick,minimum width=4pt,minimum height=4pt] {\thesection};
\end{tikzpicture}
}
{0.4em}
{}

\begin{document}
\section{Test Section}
\blindtext[2]
\end{document}


And here is the output: Which is great and all, but it would be nice if I could shift that box so that it is either level or inline with the section name horizontally. Is there a way to do this?

• I have given the answer without fontspec--you can add it if desired – js bibra Jul 21 at 13:39
• a tikz solution is also added – js bibra Jul 21 at 14:28
• --were you able to have a look at the edited answer – js bibra Jul 21 at 23:48

I have commented out fontspec to avoid lua compilation and instead of tikz I used tcolorbox which works fine --use the baseline option for shifting the number box up or down

\documentclass[10pt]{article}

\usepackage{tikz}
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage[
a4paper,
twoside,
portrait,
top=1.6cm,
bottom=1.6cm,
left=0.75in,
right=0.75in,
footskip=1.4cm, % Space from the bottom margin to the baseline of the footer
headsep=10pt, % Space from the top margin to the baseline of the header
heightrounded
]{geometry}
\usepackage{blindtext}
\usetikzlibrary{calc}
%\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{tcolorbox}
%\setmainfont[
%    BoldFont = Roboto-Bold
%]{Roboto}
%\defaultfontfeatures{Ligatures=TeX}
%\defaultfontfeatures{Scale = MatchLowercase}

%\titleformat{\section}
%    {\Large\bfseries\color{black}}
%    {
%    \begin{tikzpicture}
%        \node (rect) at (0pt,0pt) [draw,
%                                   thick,
%                                   minimum width=4pt,
%                                   minimum height=4pt,
%%                                  remember picture,
%                                   baseline={([yshift=-15pt]\thesection.east)}] {\thesection};
%    \end{tikzpicture}
%    }
%    {0.4em}
%    {}

\titleformat{\section}
{\normalfont\Large\sffamily\bfseries\color{black}}
{\tcbox[colback=blue!30, colframe=blue!50, coltext=black, on line, boxsep=0pt, left=4pt, right=4pt, top=4pt, bottom=4pt,baseline=2pt]{\thesection}}{0.4em}{}

\begin{document}
\section{Test Section}
\blindtext[2]
\end{document}


edit --using tikz as a solution

\documentclass[10pt]{article}

\usepackage{tikz}
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage[
a4paper,
twoside,
portrait,
top=1.6cm,
bottom=1.6cm,
left=0.75in,
right=0.75in,
footskip=1.4cm, % Space from the bottom margin to the baseline of the
footer
headsep=10pt, % Space from the top margin to the baseline of the header
heightrounded
]{geometry}
\usepackage{blindtext}
\usetikzlibrary{calc}
%\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{tcolorbox}
%\setmainfont[
%            BoldFont = Roboto-Bold
%            ]{Roboto}
%\defaultfontfeatures{Ligatures=TeX}
%\defaultfontfeatures{Scale = MatchLowercase}

\titleformat{\section}
{\Large\bfseries\color{black}}
{
\begin{tikzpicture}
\node (rect) at (0pt,0pt) [draw,
thick,
minimum width=4pt,
minimum height=4pt,
remember picture,
overlay,
baseline=
(rect.base),
anchor=base,yshift=4pt]
{\thesection};
\end{tikzpicture}
}
{0.4em}
{}

\begin{document}
\section{Test Section}
\blindtext[2]
\end{document}

• I think using tcolorbox is a good solution too, but I wanted to see if I could do the same in tikz as well. Thanks for the answer though! – Kookie Jul 21 at 13:45