如何使自定义部分标签与名称一致?

如何使自定义部分标签与名称一致?

因此,我决定尝试使用 TikZtitlesec自定义节标题设计。这是我目前所做的。

\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,
    headheight=15pt,
    includehead, includefoot,
    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}

输出如下:瓦乌 这很棒,但如果我可以移动该框,使其与部分名称水平对齐或水平对齐,那就太好了。有办法吗?

答案1

我已经注释掉 fontspec 以避免 lua 编译,并且我使用了 tcolorbox 而不是 tikz,效果很好——使用基线选项将数字框向上或向下移动

在此处输入图片描述

\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,
    headheight=15pt,
    includehead, includefoot,
    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}

编辑——使用 tikz 作为解决方案

在此处输入图片描述

\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,
    headheight=15pt,
    includehead, includefoot,
    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}

相关内容