새 명령을 사용하면 페이지 번호가 재설정됩니다.

새 명령을 사용하면 페이지 번호가 재설정됩니다.

나는 어떤 것이 n번 반복되도록 허용하는 명령을 서문에 정의했습니다.

\def\myrepeat#1#2{\count0=#1 \loop \ifnum\count0>0 \advance\count0 by -1 #2\repeat}

따라서 \myrepeat{3}{foo}는 foofoofoo를 입력하는 것과 같습니다.

본문에서 \myrepeat 명령을 사용할 때마다 페이지 번호가 0으로 재설정됩니다. 왜 그런 일이 발생하며, 이 문제를 쉽게 해결할 수 있는 방법이 있습니까?

다음은 문제를 보여주는 예입니다.

\documentclass[]{article}

\def\irepeat#1#2{\count0=#1 \loop \ifnum\count0>0 \advance\count0 by -1 #2\repeat}

\begin{document}
The page number here is 1.
\newpage
\irepeat{3}{foo}
The page number here is 0.
\newpage
And 1 again.
\end{document}

감사합니다.

답변1

카운터는 \count0현재 페이지 번호를 저장하므로 임시 저장용으로 사용하는 것은 좋지 않습니다. 코드를 그룹으로 묶어서 할 수도 있지만, 무언가를 인쇄해야 하는 경우에는 권장되지 않습니다. 왜냐하면 단락을 인쇄하면 예상치 못한 시간에 페이지 빌더가 실행되어 페이지에 잘못된 번호가 부여될 수 있기 때문입니다.

LaTeX는 임시 저장을 위한 두 개의 카운터를 제공 \@tempcnta합니다 \@tempcntb. \count@와 같은 것을 사용할 수도 있습니다 \count255.

토큰 목록의 반복 사본을 인쇄하는 더 좋은 방법이 있습니다. 가장 간단한 것은 LaTeX3 기능을 사용하는 것입니다.

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\irepeat}{ m m }
{
 \prg_replicate:nn { #1 } { #2 }
}
\ExplSyntaxOff

\begin{document}

\irepeat{3}{foo}

\end{document}

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

\numexpr표준 LaTeX 기술을 사용 하면

\documentclass{article}

\makeatletter
\newcommand{\irepeat}[2]{%
  \ifnum#1>\z@
    #2%
    \expandafter\@firstofone
  \else
    \expandafter\@gobble
  \fi
  {\expandafter\irepeat\expandafter{\number\numexpr#1-1\relax}{#2}}%
}
\makeatother

\begin{document}

\irepeat{3}{foo}

\end{document}

루프 포함

\documentclass{article}

\makeatletter
\newcommand\irepeat[2]{%
  \@tempcnta=#1\relax % or \@tempcntb or \count@
  \loop\ifnum\@tempcnta>\z@
    \advance\@tempcnta by \m@ne
    #2%
  \repeat
}
\makeatother

\begin{document}

\irepeat{3}{foo}

\end{document}

관련 정보