라벨이 없는 참조 섹션이나 장?

라벨이 없는 참조 섹션이나 장?

모든 섹션과 하위 섹션에 수동으로 라벨을 지정하지 않고도 문서의 장과 섹션을 참조하는 것이 가능합니까?

약 8개의 챕터와 수십 개의 섹션/하위 섹션이 그 안에 들여쓰기되어 있는데, 각 섹션에 라벨을 붙이는 것은 꽤 지루합니다.

더 빠르고 편리한 방법이 있나요?

답변1

이는 가능하지만 위험할 수 있습니다. 다음 예에서는 각 장이나 섹션의 첫 번째 단어를 사용하여 \chapter또는 를 호출할 때마다 자동으로 레이블을 만듭니다 \section. titlesec해당 explicit옵션을 사용하여 타이틀을 획득했습니다. 를 사용하면 xstring첫 번째 단어가 추출됩니다.

\documentclass[10pt]{book}
\usepackage[explicit]{titlesec}
\usepackage{xstring}

\titleformat{\chapter}[display]
{\normalfont\huge\bfseries}{\chaptertitlename\ \thechapter}{20pt}{\Huge\StrBefore{#1}{ }[\mlabel]#1\label{\mlabel}}
\titleformat{\section}
{\normalfont\Large\bfseries}{\thesection}{1em}{\StrBefore{#1}{ }[\mlabel]#1\label{\mlabel}}

\begin{document}

Some references to chapters: \ref{Some} and \ref{Other} and some references to sections: \ref{A} and \ref{Another}
\chapter{Some Chapter}
\section{A Test Section}
\chapter{Other Chapter}
\section{Another Test Section}

\end{document}

여기에 이미지 설명을 입력하세요

두 개의 섹션 단위 제목에 동일한 초기 단어가 있으면 실패하지만 다른 문자열을 선택하여 레이블을 작성할 수 있습니다.

초기 솔루션에서는 제목에 공백이 하나 이상 포함되어 있다고 가정했지만 다른 문자열을 사용할 수도 있습니다. 예를 들어 (다음과 같이nicolai.rostov님의 답변) 단면 단위의 전체 이름입니다. 이 경우 xstring 패키지는 더 이상 필요하지 않으며 간단하게 말할 수 있습니다.

\documentclass[10pt]{book}
\usepackage[explicit]{titlesec}

\titleformat{\chapter}[display]
{\normalfont\huge\bfseries}{\chaptertitlename\ \thechapter}{20pt}{\Huge#1\label{#1}}
\titleformat{\section}
{\normalfont\Large\bfseries}{\thesection}{1em}{#1\label{#1}}

\begin{document}

Some references to chapters: \ref{Some Chapter} and \ref{Other Chapter} and some references to sections: \ref{A Test Section} and \ref{Another Test Section}
\chapter{Some Chapter}
\section{A Test Section}
\chapter{Other Chapter}
\section{Another Test Section}

\end{document}

내 예에서는 장과 섹션에 대한 메커니즘만 보여 주지만 다른 섹션 단위로 쉽게 확장할 수 있습니다.

답변2

당신은 사용할 수 있습니다전체 제목장이나 섹션을 레이블로 지정하고 해당 제목을 통해 다시 참조할 수 있습니다.

\documentclass{book}

\let\origchapter=\chapter
\let\origsection=\section

\renewcommand\chapter[1]{\origchapter{#1}\label{#1}}    
\renewcommand\section[1]{\origsection{#1}\label{#1}}

\begin{document}

\chapter{My first chapter}

\section{My first section}

I said in section~\ref{My first section} at the
beginning of chapter~\ref{My first chapter} that

\end{document}

답변3

라벨 생성 프로세스를 자동화할 수 있습니다. 매우 기본적인 Perl 스크립트로 이를 수행할 수 있습니다. Perl 스크립트는 모든 .tex파일을 글로브링하고 해당 파일에 대해 대체 정규식을 하나씩 실행해야 합니다. 다음은 사용해야 하는 정규식의 예입니다.

my $input_str = do{ local $/; <> };
$input_str =~ s/(\\section{(.*?)}(?!\s*\\label))/$1\n\\label{sec:$2}/mg;
$input_str =~ s/(\\chapter{(.*?)}(?!\s*\\label))/$1\n\\label{chap:$2}/mg;
print $input_str;

\chapter{Chapter Title}이 두 정규식은 단순히 각각 및 \section{Section Name}tex 코드를 찾습니다 . 그 이후에 정의된 레이블이 있으면 스크립트는 이를 무시합니다. 그렇지 않은 경우 스크립트는 \label{chap:Chapter Title}및 를 읽는 새 줄에 해당 라벨 문을 추가합니다 \label{sec:Section Name}. 이 perl 스크립트를 생성하고 새 섹션/챕터를 생성할 때마다 실행할 수 있습니다.

면책조항:나는 귀하의 파일에 대해 정규식을 실행할 때 발생할 수 있는 데이터 손실에 대해 책임을 지지 않습니다 .tex. 위의 정규식을 테스트했지만 그러한 위험을 피하기 위해 의도적으로 완전한 Perl 스크립트에 대한 코드를 포함하지 않았습니다. 내 Perl 코드를 스크립트에 잘라내어 붙여넣고 파일에서 실행하면 작동 중인 Perl 코드를 계속 볼 수 있습니다 .tex. 파일을 수정하지 않고도 화면에 결과가 작동하고 인쇄됩니다. 자신만의 Perl 스크립트를 작성하고 더미 .tex파일에서 테스트하세요. 또한, 그러한 스크립트를 실행하기 전에 항상 작업 복사본을 가져가십시오.

관련 정보