파일의 문서에 버전 번호 추가

파일의 문서에 버전 번호 추가

나는 해결책을 찾기 위해 미친 듯이 노력했습니다. 기본적으로 제목 페이지 어딘가에 문서의 버전 번호를 추가해야 합니다. 내가 원하는 것은 version.tex빌드 번호(예: 23)가 포함된 파일을 갖고, pdf생성될 때마다 파일에서 카운터를 읽고 증가한 다음 다시 저장하는 것입니다. 이렇게 하면 문서 번호가 늘어나고 다양한 버전을 추적할 수 있습니다. 예를 들면 다음과 같습니다.

\documentclass[]{article}
% Initialize the counter
\newcounter{versionNumber}
% Set it to the current value THIS FAILS
\setcounter{versionNumber}{\input{version.tex}}
%Increase by one
\stepcounter{versionNumber}
% Write to the file the new number
% MISSING


\begin{document}

Version of the document: \theVersionNumber

\end{document}

패키지를 살펴봤지만 gitinfo2너무 복잡해 보입니다. 도움을 주시면 감사하겠습니다.

답변1

Ulrike Fischer가 말했듯이: 카운터는 컴파일 시간마다 증가하지만 이는 기본적으로 쓸모가 없습니다. 컴파일은 변경되지 않은 버전에서도 작동하며, 해당 버전에는 일부 버전 번호가 있어야 합니다.

그러나 요청한 기능은 다음과 같습니다.

문서 시작 부분에서는 카운터가 증가하고 끝 부분에서는 현재 값이 파일 .aux( \@auxout)에 저장됩니다. 이 파일은 행위 이전에 읽혀지기 때문에 \begin{document}이미 카운터 값이 있으므로 행위 versionNumber\stepcounter수 있습니다.


\documentclass[]{article}
% Initialize the counter
\newcounter{versionNumber}



\makeatletter 
% At the end write the current value back to the `.aux` file
\AtEndDocument{%
  \immediate\write\@auxout{%
    \string\setcounter{versionNumber}{\number\value{versionNumber}}%
  }%
}

\makeatother
% Step the counter at the beginning
\AtBeginDocument{%
   \stepcounter{versionNumber}
}



\begin{document}

Version of the document: \theversionNumber

\end{document}

편집하다또 다른 접근법

예를 들어 실제 버전 소프트웨어에 의해 업데이트되는 다른 외부 파일과 \readin라인을 매크로에 저장하는 하위 수준 명령을 사용하십시오 \versioninfo.

\documentclass[]{article}

\newcommand{\versionfilename}{\jobname.vers}

\newwrite\versionfile

\AtBeginDocument{%
  \immediate\openin\versionfile=\versionfilename
  \read\versionfile to \versioninfo % \versioninfo is the macro containing the text line
\immediate\closein\versionfile% 
}





\begin{document}

Version of the document: \versioninfo

\end{document}

\jobname.vers예를 들어 다음 줄이 포함되어 있습니다

42 Gandalf 04/22/2015 16:03

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

답변2

나는 조금 늦었다는 것을 알고 있지만 나 자신에게 똑같은 질문을 하고 있었고 깔끔한 작은 패키지를 발견했습니다.버전.

MWE:

\documentclass{article}

\usepackage{mversion} %Provides version with build number.
\setVersion{0.1} %Sets the "official" version number.
\increaseBuild %Increases the build number at each build.

\begin{document}

\title{My Document Title}
\author{Author's Name}
\date{\today \\ v\version} %using the version number in the date for example
\maketitle

\thispagestyle{empty} %Removes page number

\end{document}

"실제" 버전 번호는 정적으로 설정되지만 빌드 번호는 동적으로 생성됩니다. 카운터를 "재설정"하려는 경우 패키지는 간단한버전.dat수동으로 편집할 수 있는 빌드 디렉터리의 파일입니다. 이제 버전 번호를 별도의 파일에 넣는 방법에 대해 설명합니다. 크고 복잡한 문서의 경우 모든 문서 설정을 다음과 같은 TeX 파일에 저장합니다.DocSettings.tex내 기본 파일로 가져옵니다.

수정된 MWE:

DocSettings.tex

\documentclass{article}

\usepackage{mversion} %Provides version with build number.
\setVersion{0.1} %Sets the "official" version number.
\increaseBuild %Increases the build number at each build.

Main.tex

\input{DocSettings}

\begin{document}

\title{My Document Title}
\author{Author's Name}
\date{\today \\ v\version}
\maketitle

\thispagestyle{empty} %Removes page number

\end{document}

2]

답변3

TeX를 실행할 때마다 다른 값을 인쇄해야 하는 경우 다음을 수행해야 합니다. 1) 파일에서 값을 읽고, 2) 값을 추가하고, 3) 값을 파일에 저장합니다. 이는 다음 코드를 통해 기본 TeX 도구(예: TeX 기본 요소와 일반 매크로)로 수행할 수 있습니다.

%% 0) declaration:
\newread\verfilein \newwrite\verfileout  \newcount\vernum

%% 1) reading the file:
\openin\verfilein=version.txt
\ifeof\verfilein \def\tmp{0}\else \read\verfilein to\tmp \fi
\closein\verfilein
\vernum=\tmp

%% 2) adding +1
\advance\vernum by1
\edef\thevernum{\the\vernum}

%% 3) saving the value to the file:
\immediate\openout\verfileout=version.txt
\immediate\write\verfileout{\the\vernum}
\immediate\closeout\verfileout

%% 4) using the value:

This document has the version \thevernum.

관련 정보