테이블 형식 환경에서 \setlength가 효과가 없는 이유는 무엇입니까?

테이블 형식 환경에서 \setlength가 효과가 없는 이유는 무엇입니까?

\setlength환경 내에서는 왜 비효율적입니까 tabular? MWE는 다음과 같습니다.

\documentclass{article}
\newlength{\globallength}
\newlength{\locallength}
\begin{document}
\setlength{\globallength}{10pt}%
\begin{tabular}{l}
\setlength{\locallength}{10pt}%

\hspace*{10pt}. \\

\hspace*{\globallength}. \\

\hspace*{\locallength}. \\

\hspace*{0pt}. \\

\end{tabular}
\end{document}

처음 두 점은 잘 정렬되어 있지만 세 번째 점은 다르며 마치 \locallength마치 것처럼 동작합니다 0pt. 왜 그렇습니까? 어떻게 해야 합니까? 환경 \setwidth내부에서 사용하고 싶기 tabular때문에 환경 외부의 길이를 설정하는 것은 실제로 선택 사항이 아닙니다.

답변1

정렬 셀은 암시적 그룹 내에서 처리되므로 그룹이 끝나면 변수에 대한 로컬 할당이 취소됩니다.

커널 정의는 \setlength다음과 같습니다.

% latex.ltx, line 2181:
\def\setlength#1#2{#1 #2\relax}

그것 때문에\global\setlength 것 같다일하다. 반면에 의 정의는 \settowidth다음과 같습니다.

% latex.ltx, line 2187:
\def\settowidth {\@settodim\wd}

그리고 의 정의 \@settodim

% latex.ltx, line 2183:
\def\@settodim#1#2#3{\setbox\@tempboxa\hbox{{#3}}#2#1\@tempboxa
       \setbox\@tempboxa\box\voidb@x}

따라서 \global\settowidth{\locallength}{abc}될 것이다

\global\setbox\@tempboxa\hbox{{abc}}\locallength\wd\@tempboxa\setbox\@tempboxa\box\voidb@x

물론 에 전역 할당을 수행하는 데는 효과가 없습니다 \locallength.

예, \settowidth{\global\locallength}{abc}효과가 있을 것입니다. 하지만 그것은 단지 우연일 뿐입니다.

LaTeX에서는 전역 차원/건너뛰기 할당을 지원하지 않으므로 낮은 수준의 명령을 사용해야 합니다. 따라서 더 안전한 방법은 새 명령을 정의하는 것입니다.

\makeatletter
\newlength\local@length@for@global
\newcommand\gsetlength[2]{%
  \setlength{\local@length@for@global}{#2}%
  \global#1\local@length@for@global
}
\newcommand{\gsettowidth}[2]{%
  \settowidth{\local@length@for@global}{#2}%
  \global#1\local@length@for@global
}
\makeatother

\gsettoheight필요한 경우 에도 마찬가지입니다 \gsettodepth.

이는 로드된 경우에도 작동하며 calc"로컬" 명령의 특정 구현을 활용하지 않습니다.

답변2

둘 다 \global\setlength{\locallength}{10pt}작동 \setlength{\global\locallength}{10pt}하는 것 같지만 왜 그것이 필요한지 모르겠습니다.

https://tex.stackexchange.com/a/210598/30810내가 그것을 시도하게 만들었고 @ChristianHupfer의 의견이 그 이유를 설명합니다.

내 긴 문서에서는 \global\settowidth{...}작동하지 않았습니다. \settowidth{\global...}했다.

관련 정보