그룹/환경 내에서 매크로 정의 재설정을 방지하는 방법

그룹/환경 내에서 매크로 정의 재설정을 방지하는 방법

질문은 다음과 같습니다. \pgPtList그룹이 종료될 때 다음 코드가 제어 시퀀스의 의미를 재설정하는 이유는 무엇입니까?

\documentclass[12pt]{article}

\begin{document}

\begingroup
    \gdef\pgPtList{}
    \edef\pgPtList{8}
    pgPtList is: \pgPtList
    
    \edef\pgPtList{8, 5}
    pgPtList is: \pgPtList
\endgroup

Empty environment has ended.

pgPtList is: \pgPtList

\end{document}

생성되는 출력은 다음과 같습니다.

pgPtList is: 8
pgPtList is: 8, 5
Empty environment has ended.
pgPtList is:

현재 제가 알고 있는 바는 이것이 그룹이 작동하는 방식과 관련이 있다는 것입니다. 따라서 후속 질문은 다음과 같습니다. 그룹이 끝난 후 변경 사항이 유지되도록 제어 시퀀스를 어떻게 재정의할 수 있습니까?

더 넓은 맥락은 다음과 같습니다. 저는 시험을 조판하고 각 페이지의 포인트 수를 추적하려고 하며, \pgPtList각 페이지에 몇 개의 포인트가 있는지 추적하기 위해 지속적으로 업데이트하고 있습니다. 각 시험에서 \pgPtList점수 값 테이블을 생성하는 다른 명령에 대한 기본 매개변수 역할을 합니다.

각 페이지의 포인트는 enumerate환경 내에서 업데이트되므로 \begingroup및 가 \endgroup표현에 존재합니다. 이상적으로 \pgPtList는 포인트 테이블 명령이 외부에서 호출되더라도 이 환경 내에서 변수를 업데이트할 수 있기를 바랍니다 .

제가 혼란스러워하는 주요 포인트는 \edef과 입니다 \gdef. 내 순진한 이해는 이러한 명령이 현재 그룹 외부에서 결과 제어 시퀀스에 액세스할 수 있도록 하기 위한 것이지만 위 코드에서 얻은 출력은 다른 일이 일어나고 있음을 나타냅니다.

답변1

  • \def\macroname<argument specification>{definition}지역 표준 정의입니다.
  • \edef\macroname<argument specification>{definition}지역적이고 확장된 정의입니다 . 즉 definition할당하기 전에 확장됩니다 .\macroname
  • \gdef\macroname<argument specification>{definition}글로벌 표준 정의입니다.
  • \xdef\macroname<argument specification>{definition}전역적이고 확장된 정의입니다.

귀하의 경우 초기 정의\gdef\pgPtList{} 하다지역 그룹에서 살아남으세요. 그렇지 않은 경우 \pgPtListafter를 사용하면 \endgroup오류가 발생합니다.

그러나 모든 후속 (재)정의는 \pgPtList명시적으로 로컬이므로 해당 변경 사항은 이후에도 유지되지 않으며 \endgroup의 의미는 \pgPtList초기 빈 정의로 되돌아갑니다.

다음 예가 도움이 될 수 있습니다.

\documentclass[12pt]{article}

\begin{document}

\newcounter{tweak}
\setcounter{tweak}{0}
\def\rstatement{Redefinition \thetweak.}

\begingroup
  \gdef\pgPtList{Initial, global definition.}
  pgPtList is: \pgPtList
  
  \edef\pgPtList{\rstatement}
  pgPtList is: \pgPtList ; \stepcounter{tweak}\pgPtList
  
  \def\pgPtList{\rstatement}
  pgPtList is: \pgPtList ; \stepcounter{tweak}\pgPtList
\endgroup

Environment has ended.

pgPtList is: \pgPtList

\begingroup
  \xdef\pgPtList{\rstatement}
  pgPtList is: \pgPtList ; \stepcounter{tweak}\pgPtList
\endgroup

Environment has ended.

pgPtList is: \pgPtList

\begingroup
  \gdef\pgPtList{\rstatement}
  pgPtList is: \pgPtList ; \stepcounter{tweak}\pgPtList
\endgroup

Environment has ended.

pgPtList is: \pgPtList

\end{document}

로컬/글로벌 및 확장/표준 매크로 정의를 보여주는 출력

매크로가 사용될 때 카운터 값을 사용하는지 확인 하십시오 \def. 이 값은 정의 당시의 값이 아닐 수도 있습니다.\gdef\pgPtListtweak\pgPtList

대조적으로, \edef그리고 그 동안 값이 변경되더라도 정의 당시의 카운터 값을 사용하는지 \xdef확인하십시오 .\pgfPtListtweak

반면에 \def및 는 모두 \edef지역 정의입니다. 해당 효과는 현재 그룹이 끝나면 종료됩니다.

대조적으로, \gdef그리고 \xdef는 글로벌합니다. \pgfPtList그룹화에 관계없이 후속 정의가 이를 대체하지 않는 한 유효합니다 .

관련 정보