尝试对齐标题部分时出现“缺少 \endcsname 插入”和其他错误

尝试对齐标题部分时出现“缺少 \endcsname 插入”和其他错误

我想以某种方式对齐章节标题:未编号的标题应居中;编号的标题应左对齐并缩进。我只是想编写一个例子

\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}

在此处输入图片描述

相关内容