Sonunda ifadeyi / makroyu değerlendirin

Sep 01 2020

Başlangıçta kaç bölüm olduğunu belirtmek istediğiniz bir belgeniz olduğunu varsayalım. Örneğin, bunun gibi bir şey:

\documentclass{article}
\begin{document}

  Abstract\\There are ... sections in this document.

  \section{Section 1}
  \section{Section 2}
  \section{Section 3}

\end{document}

Burada ... 3 bölüm olduğunu söyleyen bir makro (veya başka bir şey) olabilir.

Maalesef, bu makroyu en sonunda değerlendirmenin bir yolunu bulamadım (3 rapor ettiği yerde).

Bu örnekte, son kısımdaki bölümlerin sayısını bilmek istiyorum. Sayaçları kullanan bir çözüm olabilir (bir şekilde), ancak gerçekten makroların değerlendirme sırasını etkileyebileceğim bir çözüm arıyorum.

Yanıtlar

3 Rmano Sep 01 2020 at 16:12

Şunları kullanabilirsiniz \AtEndDocument(ve \AtBeginDocumentmakroyu ilk çalıştırmada ayarlamak için):

\documentclass{article}

\makeatletter
\AtEndDocument{
    \write\@auxout{\string\gdef\string\previousrunsections{\thesection}}%
}
\AtBeginDocument{%
    \ifcsname previousrunsections\endcsname
    \else
        \gdef\previousrunsections{??}%
    \fi
}
\makeatother
\begin{document}

  Abstract
  
  \noindent There are \previousrunsections{} sections in this document.

  \section{Section 1}
  \section{Section 2}
  \section{Section 3}

\end{document}

En az iki koşudan sonra şunları elde edersiniz:

Daha fazla kontrole ihtiyacınız varsa, paket etoolboxsize çok sayıda kanca sağlar.

PD: \\Normal metinde satırların veya paragrafların sonlarında kullanmayın !

6 egreg Sep 01 2020 at 17:19

Kullanarak toplam sayıyı istediğiniz yere koyabilirsiniz totcount.

\documentclass{article}
\usepackage{totcount}

\regtotcounter{section}

\begin{document}

\title{Title}
\author{Ömer}

\maketitle

\begin{abstract}
This is the abstract.

There are \total{section} sections in this document.
\end{abstract}

\section{Section 1}

\section{Section 2}

\section{Section 3}

\end{document}

2 StevenB.Segletes Sep 01 2020 at 16:33

Bu xyz, bilgileri kaydetmek için bir aux dosyası kullanır .

\documentclass{article}
\newcommand\addxyzline[1]{\addtocontents {xyz}{#1}}
\makeatletter
\newcommand\writexyz{\@starttoc{xyz}}
\makeatother
\begin{document}
%\tableofcontents% CAN UNCOMMMENT TO SEE THAT toc WORKS FINE
\noindent Abstract\\There are \writexyz sections in this document.

\section{Introduction}
\section{Next}
\section{Third}
\addxyzline{\thesection}
\end{document}

Derlemenin ardından, .xyzdosya bu durumda numarayı içerir 3ve .auxdosya şunları içerir:

\relax 
\@writefile{toc}{\contentsline {section}{\numberline {1}Introduction}{1}\protected@file@percent }
\@writefile{toc}{\contentsline {section}{\numberline {2}Next}{1}\protected@file@percent }
\@writefile{toc}{\contentsline {section}{\numberline {3}Third}{1}\protected@file@percent }
\@writefile{xyz}{3}

Çıktı böylece:

Not: Verilen sürüm, girdi dosyanızın adı ne olursa olsun çalışır. Eğer toc yaklaşımı ile çalışmamayı tercih ediyorsanız, onu belge adınıza bağlayabilirsiniz.

\newcommand\writexyz{\input junk.xyz }

bu durumda belge junk.tex olmalıdır.