
와 비슷한 매크로가 필요합니다 \providecommand
. \providelength{\lengthName}{0.5cm}
아직 존재하지 않는 경우 새로운 길이를 알려주어야 합니다. 원하는 값이 제공되지 않은 경우 매크로를 사용하여 TikzPicture의 표준 값을 설정하고 싶습니다. 웹에서 다음 코드를 찾았지만 작동하지 않습니다.
\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\makeatletter
\newcommand*\providelength[1]{%
\begingroup
\escapechar\m@ne
\xdef\@gtempa{\string#1}%
\endgroup
\@ifundefined{\@gtempa}%
{\newskip#1}%
{}%
}
\makeatother
\begin{document}
\providelength{\ltest}{0.1pt}
\the\ltest
\end{document}
이 문서의 출력은 다음과 같습니다
0.1pt
0.0pt
따라서 이 명령은 전혀 효과가 없는 것 같습니다. 대신 인수가 인쇄됩니다. 누구든지 이 문제를 해결하는 방법을 알고 있습니까?
답변1
\
내 솔루션은 길이 레지스터 이름의 시작 부분에 문자가 있다고 가정하고 테스트되지 않습니다 tikz
.
\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{etoolbox}%
\usepackage{ifmtarg}%
\makeatletter
\newcommand*{\otherprovidelength}[2]{%
\begingroup
\escapechar\m@ne
\xdef\@gtempa{\string#1}%
\endgroup
\@ifundefined{\@gtempa}%
{\newskip#1%
#1=#2}% Assign the 2nd argument.
{}%
}
\makeatother
\providecommand{\providelength}[2]{%
\ifdeflength{#1}{% It is already defined!
}{% Not defined, so define it!
\newlength{#1}%
}%
\setlength{#1}{#2}%
}%
\makeatletter
\providecommand{\ProvideLength}[2][]{%
% Check, if the command is already defined, if not, then define it!
\ifdeflength{#2}{% It is already defined!
\GenericWarning{}{Warning: Length #2 already defined!!!!!!!!} % Optional
}{% Not defined, so define it!
\newlength{#2}%
}%
\@ifmtarg{#1}{% is 1st argument empty -> do not set the length at all!
}{% Set the length to the value of the 1st argument.
\setlength{#2}{#1}%
}% End of \@ifmtarg
}% End of \providecommand
\makeatother
\begin{document}
\providelength{\ltest}{0.01pt}
\the\ltest \par
% Some testing
\addtolength{\ltest}{0.05pt}
\the\ltest \par
\ProvideLength[0.17pt]{\ltesttwo}
\ProvideLength{\lyetanotherlength} % initialized to 0.0pt if undefined before
\the\ltesttwo \par
\the\lyetanotherlength
\end{document}
편집하다:
\ProvideLength
길이 값을 선택적 첫 번째 인수로 사용하고 길이 레지스터 이름을 두 번째 인수로 사용하는 또 다른 명령을 추가했습니다 . 길이 레지스터가 이미 존재하는 경우에도 경고가 생성됩니다.
나는 Simon의 명령을 다음으로 변경 \otherprovidelength
하고 누락된 두 번째 인수와 건너뛰기 레지스터에 대한 할당을 추가했습니다.