.
.
这家伙很懒,什么也没写!

注册于 1年前

回答
5
文章
0
关注者
0

\documentclass[UTF8,oneside]{ctexbook}
\usepackage{zhlipsum}
\usepackage{geometry}
\usepackage{xcolor}
\usepackage{pagecolor} 
\usepackage{layout}
\usepackage{chemfig,subcaption,graphicx,float}
\usepackage{listings}
\usepackage{titletoc}
\usepackage{titlesec}
\usepackage{fontspec}
\definecolor{藏花红}{RGB}{236,45,122}
% 强制框架宽度为 \textwidth
\titleformat*{\section}{\makebox[\textwidth][c]} 
\titleformat{\section}[frame]
    {\color{藏花红}}
    {\centering\footnotesize\enspace 节 \thesection\enspace}
    {8pt}
    {\bfseries\filcenter\songti}
    [\vspace{-10pt}]

% 代码排版配置
% 自定义VBA语法高亮
\usepackage{listings}
\usepackage{xcolor}

% 定义VBA高亮样式
\lstdefinelanguage{VBA}{
    morekeywords={Sub, End, Dim, As, Set, Range, Worksheet, MsgBox, If, Then, Else, For, Next, Do, While, Loop, Integer, String},
    sensitive=true,
    morecomment=[l]{'},       % 单行注释
    morestring=[b]{"},        % 字符串定义
    keywordstyle=\color[HTML]{0184bc},
    stringstyle=\color[HTML]{41a977},
    basicstyle=\small\ttfamily, % 等宽字体
    keepspaces=true,
    numbers=left,
    %framexleftmargin=1em,    % 左边框到代码内容的距离
   % framexrightmargin=1.5em,   % 右边框到代码内容的距离
  % xleftmargin=2em,           % 代码内容左缩进(边框内侧)
   %xrightmargin=2em,          % 代码内容右缩进(边框内侧)
    columns=fullflexible       % 非等宽对齐
}

% 全局设置:代码框总宽度 = \textwidth
\lstset{
    language=VBA,
    frame=single,
    linewidth=\dimexpr\textwidth \relax, % 内容宽度 = 总宽度 - (左+右)边框间距
    escapeinside=``                          % 中文注释处理
}



\begin{document}
\section{初步}
\begin{lstlisting}[caption={生成斐波那契数列},label={code:fib}]
'斐波那契数列生成函数
Sub GenerateFibonacci()
\end{lstlisting}
\end{document}


linewidth指文本宽度,默认值\linewidth
那么\linewidth 是什么呢
在LaTeX中,\linewidth 是一个动态的长度参数,表示​​当前环境下的可用行宽度​​。它的值会根据所在的环境自动调整,使得内容能够自适应不同的布局。以下是关键点总结:
​​动态特性​​:
\linewidth 的值在进入不同环境(如minipage、itemize、multicol等)时会自动更新,退出环境后恢复原值。
例如,在 minipage{0.5\textwidth} 内部,\linewidth 会变为 0.5\textwidth,方便内容自动适应容器宽度。

对比其他宽度参数​​:
​​\textwidth​​:整个页面的文本区域宽度(固定值,由文档类和页边距决定)。
​​\columnwidth​​:在多栏布局中单栏的宽度(与\linewidth在单栏中通常相等)。
​​\hsize​​:底层TeX参数,一般不建议直接修改。

​​常见使用场景​​:
​​图片插入​​:\includegraphics[width=\linewidth]{...} 让图片宽度适配当前环境。
​​表格或浮动体​​:设置表格或浮动体宽度为当前行宽,增强布局灵活性。
​​嵌套环境​​:在列表、缩进或自定义盒子中,\linewidth 自动减去缩进或边距。

framexleftmargin=1.5em,    左边框到代码内容的距离,只调整这个,行号位置是不变的区别于下面

左右额外边距(Extra Margins)​​
​​参数​​:
xleftmargin:代码内容左侧的额外边距(从页面左边缘到代码内容的距离)。
xrightmargin:代码内容右侧的额外边距(从代码内容到页面右边缘的距离)。
​​作用​​:
这两个参数会在代码内容的 ​​左右两侧添加额外的空白区域​​,类似于在代码块周围“填充”空白。
此边距会影响 ​​整个代码块​​(包括边框和行号)。
如果设置了 frame=single(边框),边框会包裹代码内容和这些额外边距。

我大胆猜想,这个框根据奇偶页数自动调整左右页边距了!我们需要让他水平居中

image.png
查字体族名,乱码
image.png
用系统的cmdimage.png
成功查询族名Source Han Serif CN,然后编译成功无报错

\documentclass[UTF8]{ctexbook}
\usepackage{titletoc}%toc:table of content
\usepackage{titlesec}
\usepackage{xcolor}
\usepackage{pagecolor} 
\usepackage{layout}%布局
\usepackage{multicol}%多栏排版宏包
\usepackage{geometry}%布局宏包
\usepackage{hyperref}%引入 hyperref 宏包以支持超链接
\usepackage{nameref}
\usepackage{tikz}  
\usepackage{lipsum} % 用于生成示例文本  
\pgfdeclarelayer{diyituceng}  
\pgfdeclarelayer{diertuceng}
\pgfsetlayers{diyituceng,main,diertuceng}
%In pgf layers are referenced using names. The standard layer, which is a bit special in certain ways, is called main. If nothing else is specified, all graphic commands are added to the main layer. You can declare a new layer using the following command:
\usetikzlibrary{shapes.geometric}

\newcommand{\tuoyvan}[1]{%椭圆
\begin{pgfonlayer}{diertuceng}
  \node[ellipse, draw, fill=white, inner sep=2pt, anchor=center] (ellipse) at (0,0) {#1};
\end{pgfonlayer}
}%
\newcommand{\jvxing}[1]{%矩形
\begin{pgfonlayer}{diyituceng}
  \node[anchor=north west, text width=\linewidth, align=center, fill=white, draw] (title) at ([xshift=-30pt]ellipse.center) {#1};
\end{pgfonlayer}
}%
%椭圆镶嵌矩形
\titleformat{\section}[block]%
  {\normalfont\Large\bfseries\begin{tikzpicture}[baseline=(ellipse.base)]}%
  {\tuoyvan{\thesection}}%
  {0pt}%
  {\jvxing}%
  [\end{tikzpicture}]%
%before-code
%after-code
%\titleformat{command}[shape]{format}{label}{sep}{before}[after]

\begin{document} 
\section{第一节}
\end{document}

df70800ed806673c6b99375b0b3fcb99.png

test.tex

完美

发布
问题