카운터를 증가시키다

카운터를 증가시키다

내가 이해하는 바에 따르면 카운터는 자동으로 증가해야 합니다. 그러나 하나를 사용하려고 하면 내가 얻는 것은 모두 0입니다. 여기 MWE가 있습니다.

\documentclass[12pt]{article}
\usepackage{lipsum}
\newcounter{mcounter}


\begin{document}
\themcounter \lipsum[1]

\themcounter \lipsum[2]

\themcounter \lipsum[3]
\end{document}

각 단락 앞에 0이 있는 인쇄된 lipsum 텍스트입니다.

사실 저는 교안 목록 중간에 페이지에 대한 카운터를 만들려고 합니다. 목록은 "1일차: 워크시트 #1, #2 및 메모. 2일: 워크시트 #3, 메모 및 워크시트 #4"와 같습니다. 워크시트를 추가하거나 제거하는 경우 해당 날짜와 이후 날짜에 대해 목록에 있는 모든 항목의 번호를 다시 매기고 싶지 않습니다.

내가 뭘 잘못하고 있는지에 대한 제안이 있습니까? 저는 Win 10 PC의 Texmaker에서 LauLaTeX를 사용하고 있습니다.

답변1

당신은 썼다,

내가 이해하는 바에 따르면 카운터는 자동으로 증가해야 합니다.

그것은 정확하지 않습니다. 카운터를 생성했지만 해당 값을 표시하는 것(예: 를 통해) 외에는 아무 작업도 수행하지 않은 경우 \themcounter카운터 값은 0문서 전체에서 초기 값(일반적으로 )으로 유지됩니다.

\setcounterLaTeX에서는 , \addtocounter, \stepcounter및 명령을 사용하여 카운터 값을 수정할 수 있습니다 \refstepcounter. 카운터 이름과 정수라는 두 가지 인수를 사용합니다 \setcounter. 그리고 카운터의 값을 로 증가시킵니다 . 그리고 값이 증가할 카운터의 이름인 하나의 인수만 사용합니다.\addtocounter\stepcounter\refstepcounter1

mycounter(a) 로 명명된 카운터를 증가시키고 1(b) 새로 증가된 의 값을 표시하는 LaTeX 매크로를 생성하려는 경우 mycounter여러 가지 방법으로 이를 수행할 수 있습니다. 예를 들어, 다음을 사용하여 카운터를 만든 후

\newcounter{mycounter}

다음 세 가지 정의 중 하나를 사용할 수 있습니다 \showmycounter.

\newcommand\showmycounter{\addtocounter{mycounter}{1}\themycounter}
\newcommand\showmycounter{\stepcounter{mycounter}\themycounter}
\newcommand\showmycounter{\refstepcounter{mycounter}\themycounter}

기본적으로 지시문 \themycounter\arabic{mycounter}동일한 출력을 생성합니다. 즉, 카운터 값을 표시하기 위해 기본적으로 아라비아 숫자가 사용됩니다. 카운터 값을 대문자 로마 숫자로 표시하려면 를 \themycounter통해 재정의하거나 \renewcommand\themycounter{\Roman{mycounter}}\newcommand지침을 변경해야 합니다. 예:

\newcommand\showmycounter{\stepcounter{mycounter}\Roman{mycounter}}

그만큼카운터의 수는 0음수를 포함한 임의의 정수일 수 있습니다. 당연히 값이 mycounter양수가 아닌 경우 해당 값을 알파벳 문자나 로마 숫자로 표시하려고 하면 오류 메시지가 생성됩니다.


다음 아이디어를 기반으로 하는 MWE(최소 작업 예제):

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

\documentclass{article}
\newcounter{mycounter} % create a new counter, called 'mycounter'
% default def'n of '\themycounter' is '\arabic{mycounter}'

%% command to increment 'mycounter' by 1 and to display its value:
\newcommand\showmycounter{\stepcounter{mycounter}\themycounter}

\usepackage{lipsum}
\newcommand\showlips{\stepcounter{mycounter}\lipsum[\value{mycounter}]}

\begin{document}
\showmycounter, \showmycounter, \showmycounter

\showlips

% verifying that the preceding command used '4':
\lipsum[4]
\end{document}

관련 정보