我怎麼才能把這個部分的風格做得更好?

我怎麼才能把這個部分的風格做得更好?

我正在製作一個模板並嘗試自訂其中的幾項內容,其中之一就是部分樣式。我創建了一個在螢幕左側製作六邊形的命令,如下圖和程式碼所示:

我使用的軟體包比這更多,但我認為解決這個問題的關鍵是這些。

\RequirePackage[%
    top =       2.5cm,    
    bottom =    3.8cm,    
    left =      2cm,    
    right =     2cm,
]{geometry}
\RequirePackage[most]{tcolorbox}
\RequirePackage{pgfplots}
\RequirePackage{graphicx}
\RequirePackage{xcolor}
\RequirePackage{tikz}
\RequirePackage{titletoc}
\RequirePackage{lmodern}
\usetikzlibrary{
    decorations.pathreplacing,
    decorations.pathmorphing,
    decorations.markings,
    shapes.multipart,
    shapes.geometric,
    arrows.meta,
    fadings,
    arrows,
    angles,
    quotes,
    calc,
    3d,
 }
\pgfplotsset{compat=1.18}
\definecolor{ChapterBackground}{HTML}{101010} % PERSONAL BLACK
\definecolor{ChapterForeground}{HTML}{e93820} % PERSONAL ORANGE I

\newcommand{\polygonSection}{%
\begin{tikzpicture}[remember picture, overlay]
\foreach \i in {0.5,...,5}{%
    \node[%
        ChapterBackground,
        regular polygon sides   = 6, 
        regular polygon,
        rounded corners,
        minimum size            = \i cm,
        ultra thick,
        draw,
    ] at ($(current page.north west |- current page text area.west)+(0,0)$) {};
}

\node[%
    regular polygon sides   = 6, 
    rounded corners,
    regular polygon,
    minimum size            = 1.58 cm,
    ultra thick,
    inner sep               = 0,
    fill                    = ChapterForeground!85,
    text                    = ChapterForeground!5,
] at ($(current page.north west |- current page text area.west)+(0,0)$) {};

\end{tikzpicture}}

在此輸入影像描述

好吧,透過這個指令,我嘗試使用 titlesec 套件中的 \titleformat{} 來設定我的部分的樣式,如下所示:

\titleformat{\section}[display]
{\normalfont\bfseries\color{ChapterBackground}}{}{1em}
{\llap{
\polygonSection\hspace{-6pt}}\huge\textsc}[]

在此輸入影像描述

但是,我想將標題與六邊形對齊並在兩者之間添加一個小空格,但我沒有找到任何方法來執行此操作,並且收到以下錯誤訊息:

套件 pgf 錯誤:沒有名為「目前頁面文字區域」的已知形狀。

但是,如果我從程式碼中刪除這部分,它看起來就不是我想要的了。另外,使用 tikzpagenodes 包,多邊形不再按部分分隔,而是按頁面分隔,這不是我想要的。

就是這樣,如果有人能幫助我,那就太好了。

答案1

我不確定縮排章節標題是否是一個好主意,以便它比正文更靠右開始。也許你應該增加左邊距。無論如何,以下內容應該滿足您的要求:

\documentclass{article}

\RequirePackage[%
    top    = 2.5cm,    
    bottom = 3.8cm,    
    left   =   2cm,    
    right  =   2cm,
]{geometry}

\RequirePackage{tikz}
\usetikzlibrary{shapes.geometric}

\definecolor{ChapterBackground}{HTML}{101010} % PERSONAL BLACK
\definecolor{ChapterForeground}{HTML}{e93820} % PERSONAL ORANGE I

\newcommand{\polygonSection}{%
\begin{tikzpicture}[remember picture, overlay] 
\node[%
    regular polygon sides   = 6, 
    rounded corners,
    regular polygon,
    minimum size            = 1.58cm,
    ultra thick,
    inner sep               = 0pt,
    fill                    = ChapterForeground!85,
    anchor                  = south
] at (current page.north west |- 0,0) (red polygon) {};
\foreach \i in {2.5,...,4.5}{%
    \node[%
        ChapterBackground,
        regular polygon sides   = 6, 
        regular polygon,
        rounded corners,
        minimum size            = \i cm,
        ultra thick,
        draw,
    ] at (red polygon) {};
}
\end{tikzpicture}}

\RequirePackage{titlesec}
\titleformat{\section}[display]
    {\normalfont\bfseries\color{ChapterBackground}\huge}{}{0pt}
    {\polygonSection\hspace{1em}}

\usepackage{lipsum}

\begin{document}

\section{Section}

\lipsum[1]

\end{document}

在此輸入影像描述

這是如何運作的?我們添加到章節標題的內容tikzpicture首先放置錨定的紅色六邊形,以便它水平位於內容的基線上tikzpicture,垂直位於紙張的左邊緣。其他六邊形(其中您實際上只需要三個,因此我調整了列表\foreach)然後以該紅色六邊形為中心。

您可以透過使用 插入一些空格來調整間距\titleformat{\section},但正如我在開頭所說的,最好一起增加文件的左邊距。

如果要將六邊形垂直居中,請anchor = south從紅色六邊形的選項中刪除並新增baseline = -0.5ex至環境的選項tikzpicture


一些註釋:tcolorbox加載tikzpgfplots也加載tikztikz加載xcolor。因此,沒有必要將所有這些套件一起加載。您還應該認真考慮哪個 Tik您真正需要的 Z 庫。對於以上,只shapes.geometric需要。

相關內容