![尝试对齐标题部分时出现“缺少 \endcsname 插入”和其他错误](https://linux22.com/image/251153/%E5%B0%9D%E8%AF%95%E5%AF%B9%E9%BD%90%E6%A0%87%E9%A2%98%E9%83%A8%E5%88%86%E6%97%B6%E5%87%BA%E7%8E%B0%E2%80%9C%E7%BC%BA%E5%B0%91%20%5Cendcsname%20%E6%8F%92%E5%85%A5%E2%80%9D%E5%92%8C%E5%85%B6%E4%BB%96%E9%94%99%E8%AF%AF.png)
我想以某种方式对齐章节标题:未编号的标题应居中;编号的标题应左对齐并缩进。我只是想编写一个例子
\documentclass[12pt,a4paper,oneside]{article}
\usepackage[left=35mm,right=15mm,top=2cm,bottom=2cm,bindingoffset=0cm]{geometry}
\usepackage[utf8x]{inputenc}
\usepackage[russian]{babel}
\usepackage{cmap}
\usepackage{makeidx}
\makeindex
\usepackage{indentfirst}
\setlength{\parindent}{1.25cm}
\usepackage{setspace}
\onehalfspacing
\usepackage{titlesec}
\titleformat{\section*}{\normalfont\Large\bfseries\justifyheading}{\thesection}{1em}{}
\begin{document}
\section*{РЕФЕРАТ}
\end{document}
但我越来越
! Missing \endcsname inserted.
! LaTeX Error: Missing \begin{document}.
! Extra \endcsname.
! Argument of \ttl@assign@i has an extra }.
! Paragraph ended before \ttl@assign@i was complete.
! Package keyval Error: \let \reserved@d =*\def \par .
! Missing \endcsname inserted.
! Missing number, treated as zero.
! Illegal unit of measure (pt inserted).
! Undefined control sequence.
! Missing number, treated as zero.
! Illegal unit of measure (pt inserted).
! Extra \endcsname.
! Undefined control sequence.
! Too many }'s.
! LaTeX Error: Command \CYRA unavailable in encoding OT1.
是的,有没有办法只对齐部分而不重新定义整个样式?我应该使用\newcommand
并将其插入到每个部分吗?
答案1
没有\justifyheading
命令;您可以改用\filcenter
;由于不同的格式将应用于编号和未编号部分,因此您可以将name=\section,numberless
的第一个参数中的选项\titleformat
用于未编号部分,而仅\section
用于编号部分。该explicit
选项用于明确访问标题并轻松实现编号部分所需的缩进:
\documentclass[12pt,a4paper,oneside]{article}
\usepackage[left=35mm,right=15mm,top=2cm,bottom=2cm,bindingoffset=0cm]{geometry}
\usepackage[utf8x]{inputenc}
\usepackage[russian]{babel}
\usepackage{cmap}
\usepackage{makeidx}
\makeindex
\usepackage{indentfirst}
\setlength{\parindent}{1.25cm}
\usepackage{setspace}
\onehalfspacing
\usepackage[explicit]{titlesec}
\titleformat{\section}
{\normalfont\Large\bfseries}{}{0em}{\hspace{1.25cm}\thesection\hspace{1em}#1}% numbered sections
\titleformat{name=\section,numberless}
{\normalfont\Large\bfseries\filcenter}{}{0em}{#1} % unnumbered sections
\usepackage{lipsum} % just to generate text
\begin{document}
\section*{РЕФЕРАТ}
\section{РЕФЕРАТ}
\lipsum[1-2]
\end{document}