我正在尝试使用 tikz 和 titlesec 包自定义文档中各章节的标题。
不幸的是,这不起作用,我需要一些帮助来解决我的问题。
我收到此错误:
! Argument of \tikz@next has an extra }.
<inserted text>
\par
l.25 \section{Problem}
I've run across a `}' that doesn't seem to match anything.
这是我的代码:
\documentclass[a4paper, 11pt]{article}
\usepackage[francais]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{titlesec}
\usepackage[x11names]{xcolor}
\usepackage{tikz}
\titleformat{\section}
{\normalfont\Large\sffamily\bfseries\color{CadetBlue4}}
{%
\tikz[anchor=base,baseline,outer sep=0pt]
\node[anchor=base,rectangle,rounded corners,fill=CadetBlue4]
{\normalfont\Large\sffamily\bfseries{\textcolor{CadetBlue2!20}{\thesection}}};%
}{0em}{}
\begin{document}
\tikz[anchor=base,baseline,outer sep=0pt]
\node[anchor=base,rectangle,rounded corners,fill=CadetBlue4]
{\normalfont\Large\sffamily\bfseries{\textcolor{CadetBlue2!20}{That works}}};
\section{Problem}
\end{document}
感谢您的帮助。
答案1
对于我来说它也运行良好,但您可以尝试以下更简单的代码来获得相同的结果tcolorbox
(无论如何依赖于tikz
):
\documentclass[a4paper, x11names, 11pt]{article}
\usepackage[francais]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tcolorbox}
\usepackage{titlesec}
\titleformat{\section}
{\normalfont\Large\sffamily\bfseries\color{CadetBlue4}}
{\tcbox[colback=CadetBlue4, colframe=CadetBlue4, coltext=CadetBlue2!20, on line, boxsep=0pt, left=4pt, right=4pt, top=4pt, bottom=4pt]{\thesection}}{0.2em}{}
\begin{document}
\section{Problem}
\end{document}