开发者

LaTeX: How to change one of section numbers to a custom letter?

I have something like this:

Section 1
...
Section 2
...
Section 3
Subsection 3.1
...
Section 4
...

And I would like to have something like this:

Section 1
...
Section 2
...
Section A
Subsection A.1
...
Section 4
...

In other words - change one of section numbers to something else 3 == A I need this for my thesis which is written in article class, and when I tried to add appendices the hyperref package broke, and "links" to section 1 directed to appendix A

edit: I made开发者_高级运维 a mistake when describing the problem, I meant that the table of contents doesn't work because LaTeX generates code (*.toc file):

\contentsline {section}{\numberline {1}}{1}{section.1}
\contentsline {section}{\numberline {2}}{1}{section.2}
\contentsline {section}{\numberline {A}}{1}{section.1}


I created the following construction, and now updated it:

Description:

A new counter for sections, which will get used only in a \begin{alphasection} ... \end{alphasection} block. Do not nest the block, or the original section number will get lost; an error message is given in this case. Each block will start recounting from "A". Original section counting continues, as this is required for HyperRef.

Put the following code in the Preamble:

\newcounter{alphasect}
\def\alphainsection{0}

\let\oldsection=\section
\def\section{%
  \ifnum\alphainsection=1%
    \addtocounter{alphasect}{1}
  \fi%
\oldsection}%

\renewcommand\thesection{%
  \ifnum\alphainsection=1% 
    \Alph{alphasect}
  \else%
    \arabic{section}
  \fi%
}%

\newenvironment{alphasection}{%
  \ifnum\alphainsection=1%
    \errhelp={Let other blocks end at the beginning of the next block.}
    \errmessage{Nested Alpha section not allowed}
  \fi%
  \setcounter{alphasect}{0}
  \def\alphainsection{1}
}{%
  \setcounter{alphasect}{0}
  \def\alphainsection{0}
}%

In document:

\section{First test}
First content
\section{Second test}
Second content
\begin{alphasection}
\section{Third test}
\subsection{Subsection test}
Content test
\section{Test Other section}
\end{alphasection}
\section{Fourth test}
Last content

Produces:

1 First test
   First content

2 Second test
   Second content

A Third test
A.1 Subsection test
   Content test

B Test Other section

5 Fourth test
   Last content

Tested, works with HyperRef.


to Sandra, I had the spacing problem when using Pindatjuh code above. It was affecting all lists. I fixed it adding "%" at the end of a couple of lines in the 3rd block of its code. Now I don't have the spacing anymore.

From:

\renewcommand\thesection{%
 \ifnum\alphainsection=1% 
   \Alph{alphasect}
 \else%
  \arabic{section}
 \fi%
}%

To:

\renewcommand\thesection{%
 \ifnum\alphainsection=1% 
   \Alph{alphasect}%
 \else
   \arabic{section}%
 \fi%
}%


The problem Karpik had (problem with hyperref) could more easily be solved by adding the option [naturalnames] to the hyperref package: \usepackage[naturalnames]{hyperref}


Ok, I solve it using @Pindatjuh code, solution is pretty ugly...

 \newcounter{alphasect}

 \renewcommand\thesection{%
 \ifnum\value{alphasect}=1%
A%%
 \else
\ifnum\value{alphasect}=2%
B%%
\else
\ifnum\value{alphasect}=3%
C%%
\else
\ifnum\value{alphasect}=4%
D%%
\else
 \arabic{section}%%
 \fi\fi\fi\fi}%

 \newenvironment{asection}{%
 \setcounter{alphasect}{1}%%
 }{%
 \setcounter{alphasect}{0}%%
 }%

 \newenvironment{bsection}{%
 \setcounter{alphasect}{2}%%
 }{%
 \setcounter{alphasect}{0}%%
 }%

a than in document:

\section{First test}
First content
\section{Second test}
Second content
\begin{asection}
\section{Third test}
\subsection{Subsection test}
Content test
\end{asection}
\begin{bsection}
\section{Test Other section}
\end{bsection}
\section{Fourth test}
Last content

now list of contents works, and it's displayed as it should


Have a look at the titlesec package.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜