I thought turning syntax on/off at appropriate places would make everything work, but I can't use \ExplSyntaxOn/Off inside \NewDocumentEnvironment so the following code sample doesn't work:
\documentclass[varwidth]{standalone}
\usepackage{expl3}
\usepackage{xparse}
\begin{document}
\NewDocumentEnvironment{myPar}{}
{
\parbox{40mm}
{% START PARBOX
\ExplSyntaxOn
\dim_new:N\myDim % DOESN'T WORK
\ExplSyntaxOff
}
{
} % END PARBOX
}
\begin{myPar}
Wikipedia is hosted by the Wikimedia Foundation, a non-profit organization that also hosts a range of other projects.
\end{myPar}
\end{document}
So I have to enclose the environment within syntax switches, as follows, but then anything passed to the environment loses its spaces:
\documentclass[varwidth]{standalone}
\usepackage{expl3}
\usepackage{xparse}
\begin{document}
\ExplSyntaxOn % BROUGHT OUTSIDE
\NewDocumentEnvironment{myPar}{}
{
\parbox{40mm}
{% START PARBOX
\dim_new:N\myDim % WORKS (BUT NO SPACES REMAIN)
}
{
} % END PARBOX
}
\ExplSyntaxOff % BROUGHT OUTSIDE
% SPACES OF THE FOLLOWING CONTENT ARE STRIPPED
\begin{myPar}
Wikipedia is hosted by the Wikimedia Foundation, a non-profit organization that also hosts a range of other projects.
\end{myPar}
\end{document}
How do I retain spaces of the passed content without replacing them with ~ symbols?
By the way, following doesn't strip spaces (but lacks \parbox as I need it):
\documentclass[varwidth]{standalone}
\usepackage{expl3}
\usepackage{xparse}
\begin{document}
\ExplSyntaxOn % BROUGHT OUTSIDE
\NewDocumentEnvironment{myPar}{}
{
\dim_new:N\myDim % WORKS (NO SPACES STRIPPED BUT NO PARBOX)
} {}
\ExplSyntaxOff % BROUGHT OUTSIDE
% NO SPACES STRIPPED FROM THE FOLLOWING CONTENT (BUT NO PARBOX)
\begin{myPar}
Wikipedia is hosted by the Wikimedia Foundation, a non-profit organization that also hosts a range of other projects.
\end{myPar}
\end{document}

\dim_new:N\myDiminside an enviroment is wrong, you will get errors as soon as you use the environment the second time. – Ulrike Fischer Jun 05 '19 at 07:56\newenvironment{foo}{\parbox{}{}}; nothing should make you think that it becomes possible withexpl3, because it is *impossible* in TeX to have a brace unbalanced token list inside the argument to a macro. – egreg Jun 05 '19 at 08:35