How to remove the last line-feed raised by \foreach
A listofitems
alternative...
\documentclass{article}
\usepackage{listofitems}
\begin{document}
\readlist*\mylist{str1,str2,str3}
\framebox{%
\parbox{4em}{%
\foreachitem\x\in\mylist[]{%
\makebox[4em][l]{\x}%
\ifnum\xcnt<\mylistlen\relax\\\fi%
}%
}%
}
\end{document}
or switch the test order:
\documentclass{article}
\usepackage{listofitems}
\begin{document}
\readlist*\mylist{str1,str2,str3}
\framebox{%
\parbox{4em}{%
\foreachitem\x\in\mylist[]{%
\ifnum\xcnt=1\relax\else\\\fi%
\makebox[4em][l]{\x}%
}%
}%
}
\end{document}
The problem is that you issue a \\
at the end of each line. You can avoid it e.g. with
\documentclass{article}
\usepackage{tikz}
\begin{document}
\def\mylist{str1,str2,str3}
\framebox{
\parbox{4em}{%
\foreach \X [count=\Y]in \mylist {%
\ifnum\Y=1
\makebox[4em][s]{\X}
\else
\\ \makebox[4em][s]{\X}%
\fi
}%
}
}
\end{document}
With a conditionnal (minimum code text)
\documentclass{article}
\usepackage{tikz}
\begin{document}
\def\mylist{str1,str2,str3}
\framebox{
\parbox{4em}{%
\foreach \x[count=\n] in \mylist {%
\ifnum \n >1 \\ \fi
\makebox[4em][s]{\x}%
}%
}
}
\end{document}
With an array (just for the fun of it)
\documentclass{article}
\usepackage{tikz}
\begin{document}
\def\mylist{str1,str2,str3}
\def\before{{"\noexpand\\",{}}}
\framebox{
\parbox{4em}{%
\foreach \x[count=\n from 0] in \mylist {%
\pgfmathparse{\before[!\n]}\pgfmathresult
\makebox[4em][s]{\x}%
}%
}
}
\end{document}
Without any conditional ....
\documentclass{article}
\usepackage{tikz}
\begin{document}
\def\mylist{str2,str3}
\framebox{
\parbox{4em}{%
str1
\foreach \x in \mylist {%
\\\makebox[4em][s]{\x}%
}%
}
}
\end{document}