LaTeX: creating new section headings

拟墨画扇 提交于 2019-12-08 04:07:17

问题


I want to create a new section like heading using a custom global counter. The intended behaviour is to have a \rule command which defines a rule, that can be used in several sections and subsection, but uses a custom global counter.

Example:

1. Section
1.1 SubSection
Rule 1: bla
Rule 2: foo
2. Section
Rule 3: foobar
2.1 subsection
Rule 4: yet another one

I tried two different things:

1.) create a custom command from scratch

\newcounter{rule}
\addtocounter{rule}{0} % set them to some other numbers than 0
\renewcommand{\rule}[1]{{\noindent\normalfont\Large\bfseries{Rule \arabic{rule}: 
#1 \addcontentsline{toc}{section}{Rule \arabic{rule}: #1}\newline\stepcounter{rule}}}}

The problem here is that I don't know how to format the heading so that it behaves like a section. Especially when the line gets wrapped.

Should be:

Rule 1: very long header line
        correctly wrapped

but it looks like this:

Rule 1: very long header line
correctly wrapped

Also the space between heading and the following text is something that should be configured correctly.

2.) I tried to change subsection for my use.

\renewcommand{\rule}[1]{\subsection{#1}}
\renewcommand{\thesubsection}{Rule \arabic{subsection}:}

This is easy and works, but has obviously several drawbacks:

  • the counter gets reset for each new section, so it's not a global counter.
  • I cannot use subsections anymore

I think only the first option is the way to go, but I don't know how. I hope anybody can help here.

Thx.


回答1:


First of all, don't redefine \rule, as it forms an integral part of other core (La)TeX functionality. Rather use something like \Rule.

Here's a straight-forward implementation for your requirements:

\documentclass{article}

\makeatletter
\newcommand\Rule{\@startsection {Rule}{1}{\z@}%
                                {-3.5ex \@plus -1ex \@minus -.2ex}%
                                {2.3ex \@plus.2ex}%
                                {\normalfont\Large\bfseries}}
\newcommand{\Rulemark}[1]{}
\newcounter{Rule}
\let\l@Rule\l@section
\begin{document}

\tableofcontents

\section{Section}
\subsection{Subsection}
\Rule{bla}
\Rule{foo}
\section{Section}
\Rule{foobar}
\subsection{Subsection}
\Rule{yet another one}

\end{document}

The above setup of \Rule is a copy of \section, with all the required amenities.



来源:https://stackoverflow.com/questions/27081715/latex-creating-new-section-headings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!