使用 tikz 编写章节编号

使用 tikz 编写章节编号

我希望roman使用来写章节编号tikz

我尝试将其加倍rule,并添加tikz节点,但是没有正常工作。

至少,这是我想要“类似”的例子。 在此处输入图片描述

我能得到的最好的就是这个 在此处输入图片描述

我的代码有点简单

\documentclass[12pt,a4paper]{report}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage[top=3cm,bottom=3cm,left=3cm,right=3cm,headheight=3cm,footskip=1cm,marginparwidth=2cm]{geometry}
\usepackage{fancyhdr}
\usepackage{lipsum}
\usepackage{mathpazo}
\usepackage{domitian}
\usepackage[T1]{fontenc}
\usepackage{setspace}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{subcaption}
\fancypagestyle{MyStyle}
{
\fancyhf{}
\fancyhead[L]{\slshape\leftmark}
\renewcommand{\headrulewidth}{2pt}
\fancyfoot[C]{\thepage}
\renewcommand{\footrulewidth}{2pt}
}
\let\oldstylenums\oldstyle

\begin{document}

\chapter{Klauss}

\begin{tikzpicture}[squarednode/.style={rectangle, draw=blue!60, fill=blue!5, very thick, minimum size=10mm},
]
\node[squarednode]      (maintopic)                              {2};
\end{tikzpicture}
\rule{\textwidth}{3pt}\vspace{-0.5cm}
\rule{\textwidth}{3pt}
\lipsum
\end{document}

有些软件包与此问题无关

答案1

编辑:请参阅以下 OP 提出的新要求的更新代码。

使用我的回答来回答类似的问题(参见https://tex.stackexchange.com/a/679614/132405),稍作修改后,我建议您使用下面的代码。

我保留了包提供的字体多米提安来自您的代码,作为章节的标题。

我已经从你的代码中删除了 tikzlibrary positioning,因为这里不需要它。

\documentclass[12pt,a4paper]{report}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage[top=3cm,bottom=3cm,left=3cm,right=3cm,headheight=3cm,footskip=1cm,marginparwidth=2cm]{geometry}
\usepackage{fancyhdr}
\usepackage{lipsum}
\usepackage{mathpazo}
\usepackage{domitian}
\usepackage[T1]{fontenc}
\usepackage{setspace}
\usepackage{tikz}

\usepackage{subcaption}
\fancypagestyle{MyStyle}
{
\fancyhf{}
\fancyhead[L]{\slshape\leftmark}
\renewcommand{\headrulewidth}{2pt}
\fancyfoot[C]{\thepage}
\renewcommand{\footrulewidth}{2pt}
}
\let\oldstylenums\oldstyle

\definecolor{myBlue}{rgb}{0,0.32,0.6}

\usepackage{titlesec}
% formatting of the chapter title (number+text)
\titleformat{\chapter}% sectionning type to customize
    [display]% shape, here number and title text on separate lines
    {\normalfont\bfseries\color{myBlue}}%format
    {\numberChapter}% formatting code of the section (here chapter) number.
    {1em}% in "display" shape, the distance between the "number" and the "title"
    {\titlerule[2.5pt]\vspace{3pt}\titlerule\vspace{4pt}\LARGE}% code before the title text. Here for rules and the title format.
    
% Setting of the space before and after the title
\titlespacing{\chapter}% Place the chapter block almost at top of the page
    {0pt}% space added at left
    {20pt}% vertical space added before
    {20pt}% vertical space added after (space between chapter title and section title, for example)

\usepackage{etoolbox}
% to correct a bug, so the chapter block is really a the top of the page (see https://tex.stackexchange.com/questions/59389/package-titlesec-adds-extra-space-on-top-of-chapter-despite-commands-to-the-co/59392#59392)
\makeatletter % the macro name contains @
\patchcmd{\ttl@mkchap@i}{\vspace*{\@tempskipa}}{}{}{}
\makeatother

\usepackage{microtype} % for the \textls command
   
\newcommand{\numberChapter}{%
\hspace*{\fill}\begin{tikzpicture}
\node[%
        fill = myBlue,
        minimum width = 4cm,
        minimum height = 4cm,
        font = \color{white}\fontsize{120}{120}\selectfont %
        ] (block) {\thechapter};
\node[%
        rotate = 90,
        anchor =  center,
        font = \color{black}\LARGE\normalfont
        ] at ([xshift=-15pt] block.west) {\textls[160]{\textsc{Chapitre}}};
\end{tikzpicture}%
}     

\pagestyle{MyStyle} %% added, so the style MyStyle that you have created is used for header and footer 

\begin{document}

\chapter{Klauss}

\lipsum
\end{document}

结果:

在此处输入图片描述

编辑:在 OP 在评论中提出要求后,有了新的布局。

通过这段新代码,我们可以看出,如果标题很长,最好不要对齐标题。我还演示了章节号的自适应形状,这样它就可以处理 9 以上的数字。

\documentclass[12pt,a4paper]{report}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage[top=3cm,bottom=3cm,left=3cm,right=3cm,headheight=3cm,footskip=1cm,marginparwidth=2cm]{geometry}
\usepackage{fancyhdr}
\usepackage{lipsum}
\usepackage{mathpazo}
\usepackage{domitian}
\usepackage[T1]{fontenc}
\usepackage{setspace}
\usepackage{tikz}

\usepackage{subcaption}
\fancypagestyle{MyStyle}
{
\fancyhf{}
\fancyhead[L]{\slshape\leftmark}
\renewcommand{\headrulewidth}{2pt}
\fancyfoot[C]{\thepage}
\renewcommand{\footrulewidth}{2pt}
}
\let\oldstylenums\oldstyle

\definecolor{myBlue}{rgb}{0,0.32,0.6}
\newlength{\chaptNumWd}
\newlength{\sepBlock}
\setlength{\sepBlock}{2em}
\newsavebox{\mybox}% for saving the chapter block, so we can obtain his width and 

\usepackage{titlesec}
% formatting of the chapter title (number+text)
\titleformat{\chapter}% sectionning type to customize
    [block]% shape, here number and title text on a block
    {\normalfont\bfseries\color{myBlue}}%format
    {}% formatting code of the section (here chapter) number.
    {0pt}% in "block" shape, the left margin    
    {\chapterFormat}% code before the title text (takes the title of the chapter as the argument)
    [\vspace{1em}{\titlerule[2.5pt]}\vspace{3pt}\titlerule\vspace{4pt}]

    
% Setting of the space before and after the title
\titlespacing{\chapter}% Place the chapter block almost at top of the page
    {0pt}% space added at left
    {20pt}% vertical space added before
    {20pt}% vertical space added after (space between chapter title and section title, for example)

\usepackage{etoolbox}
% to correct a bug, so the chapter block is really a the top of the page (see https://tex.stackexchange.com/questions/59389/package-titlesec-adds-extra-space-on-top-of-chapter-despite-commands-to-the-co/59392#59392)
\makeatletter % the macro name contains @
\patchcmd{\ttl@mkchap@i}{\vspace*{\@tempskipa}}{}{}{}
\makeatother

\usepackage{microtype} % for the \textls command
   
\newcommand\chapterFormat[1]{%
\sbox{\mybox}{%
\begin{tikzpicture}[outer sep=0pt]%
\node[%
        fill = myBlue,
        minimum width = 4cm,
        minimum height = 4cm,
        inner xsep=1.2em,
        font = \color{white}\fontsize{120}{120}\selectfont %
        ] (block) {\thechapter};
\node[%
        rotate = 90,
        inner xsep=0pt,
        anchor =  center,
        font = \color{black}\LARGE\normalfont
        ] at ([xshift=-15pt] block.west) {\textls[140]{\textsc{Chapitre}}};
\end{tikzpicture}%
}%
\settowidth{\chaptNumWd}{\usebox{\mybox}}%
\parbox[b]{\dimexpr\textwidth-\chaptNumWd-\sepBlock}{\LARGE#1}\hfill\usebox{\mybox}%
}     

\pagestyle{MyStyle} %% added

\begin{document}
\chapter{Klauss}

\lipsum[1-2]

\setcounter{chapter}{9}
\chapter{Équations intégro-différencielles du premier ordre}

\section{Section Un}

\lipsum
\end{document}

在此处输入图片描述

在此处输入图片描述

上面的情况是“极端的”,因为我使用了一个在开头没有连字符的单词(“diffé”),导致水平盒子未满。

这可以用不规则的右标题进行修改。

更换:

\parbox[b]{\dimexpr\textwidth-\chaptNumWd-\sepBlock}{\LARGE#1}\hfill\usebox{\mybox}%

\parbox[b]{\dimexpr\textwidth-\chaptNumWd-\sepBlock}{\raggedright\LARGE#1}\hfill\usebox{\mybox}%

我们获得:

在此处输入图片描述

相关内容