I need to get the first length in a list of lengths and store it in a variable.
This is what I got so far:
\documentclass[12pt,a4paper]{article}
\newlength{\mylength}
%% does not work at all
%\usepackage{xstring}
%\newcommand{\mylengthlist}[1]{
% \noexpandarg%
% \def\firstitem{\StrBefore{#1}{,}}%
% \setlength{\mylength}{\firstitem}%
%}
% works, but this seems to much processing for this task
\usepackage{xparse}
\NewDocumentCommand{\mylengthlist}{
>{\SplitList{,}} m
}{
\thisisthefirsttrue
\ProcessList{#1}{\myfirst}
}
\newif\ifthisisthefirst
\newcommand{\myfirst}[1]{
\ifthisisthefirst
\setlength{\mylength}{#1}
\thisisthefirstfalse
\fi
}
\begin{document}
\mylengthlist{2in, 2cm, 10pt}
Test 1: \rule{\mylength}{1pt}\par
\mylengthlist{0.8\textwidth, 2cm, 10pt}
Test 2: \rule{\mylength}{1pt}
\end{document}
The macro \mylengthlist
gets a comma separated list of lengths (one or more) and sets \mylength
to the value of the first item.
Unfortunately I think I'm doing too much processing just to get the first item.
I tried using the package xstring
, but what I get is not recognized as a length by \setlength
.
I hope someone can help me with a better solution.