Procurando por \ providelength

Procurando por \ providelength

Eu preciso de uma macro semelhante a \providecommand. \providelength{\lengthName}{0.5cm}deve me dar um novo comprimento se ainda não existir. Gostaria de usar a macro para definir valores padrão para um TikzPicture se nenhum valor desejado for fornecido. Encontrei o seguinte código na web, mas não está funcionando:

\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}

A saída deste documento é

0.1pt
0.0pt

Portanto, o comando parece não ter efeito algum. Em vez disso, o argumento é impresso. Alguém tem uma ideia de como resolver isso?

Responder1

Minha solução assume que há um \caractere no início do nome do registro de comprimento, para o qual não foi testado, tikzetc.

\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}

EDITAR:

Adicionei outro comando \ProvideLengthque utiliza o valor do comprimento como primeiro argumento opcional e os nomes do registro de comprimento como segundo argumento. Também irá gerar um aviso se o registro de comprimento já existir.

Mudei o comando de Simon para \otherprovidelengthe adicionei o segundo argumento que faltava, bem como a atribuição ao registro de salto.

informação relacionada