0

In this answer, I was provided with the following code that solves my original problem.

\ExplSyntaxOn

\NewDocumentEnvironment{withalignment}{m}
 {
  \begin{\vincent_alignment:n { #1 }}
 }
 {
  \end{\vincent_alignment:n { #1 }}
 }

\cs_new:Nn \vincent_alignment:n
 {
  \str_case:nn { #1 }
   {
    {c}{center}
    {center}{center}
    {l}{flushleft}
    {left}{flushleft}
    {r}{flushright}
    {right}{flushright}
   }
 }
\ExplSyntaxOff

that can be used this way:

\begin{withalignment}{left}
\lipsum[1][1-4]
\end{withalignment}

\begin{withalignment}{center}
\lipsum[1][1-4]
\end{withalignment}

The problem, however is when I provide the alignment through a macro/command:

\newcommand{\myalignment}{center}
\begin{withalignment}{\myalignment}
\lipsum[1][1-4]
\end{withalignment}

It does not seem to work. How to fully expand the argument before processing it so that it works?

4
  • \str_case:en, or generate a e-variant for \vincent_alignment:n. Commented Sep 19, 2023 at 15:58
  • @UlrikeFischer Could you provide code, expl3 being an alien language to me?
    – Vincent
    Commented Sep 19, 2023 at 16:00
  • In your code, just exchange str_case:nn with str_case:en.
    – lukeflo
    Commented Sep 19, 2023 at 16:57
  • Oh, @muzimuzhiZ was faster :)
    – lukeflo
    Commented Sep 19, 2023 at 16:57

1 Answer 1

2

Changes I made:

  • [required] In defining \vincent_alignment:n, \str_case:nn is replaced with \str_case:en
  • [optional] In defining withalignment environment, an \exp_args:Ne is added in front of \begin and \end respectively. This change is optional, but should make withalignment more efficient.
\documentclass{article}
\usepackage{lipsum}

\ExplSyntaxOn
\NewDocumentEnvironment{withalignment}{m}
 {
  \exp_args:Ne \begin {\vincent_alignment:n { #1 }}
 }
 {
  \exp_args:Ne \end {\vincent_alignment:n { #1 }}
 }

\cs_new:Nn \vincent_alignment:n
 {
  \str_case:en { #1 }
   {
    {c}{center}
    {center}{center}
    {l}{flushleft}
    {left}{flushleft}
    {r}{flushright}
    {right}{flushright}
   }
 }
\ExplSyntaxOff

\begin{document}
\begin{withalignment}{left}
\lipsum[1][1-4]
\end{withalignment}

\begin{withalignment}{center}
\lipsum[1][1-4]
\end{withalignment}

\newcommand{\myalignment}{center}
\begin{withalignment}{\myalignment}
\lipsum[1][1-4]
\end{withalignment}

\renewcommand{\myalignment}{right}
\begin{withalignment}{\myalignment}
\lipsum[1][1-4]
\end{withalignment}
\end{document}

enter image description here

Bonus: Applying the second solution Ulrike Fischer suggested in question comment

\documentclass{article}
\usepackage{lipsum}

\ExplSyntaxOn
\NewDocumentEnvironment{withalignment}{m}
 {
  \exp_args:Ne \begin {\vincent_alignment:e { #1 }}
 }
 {
  \exp_args:Ne \end {\vincent_alignment:e { #1 }}
 }

\cs_new:Nn \vincent_alignment:n
 {
  \str_case:nn { #1 }
   {
    {c}{center}
    {center}{center}
    {l}{flushleft}
    {left}{flushleft}
    {r}{flushright}
    {right}{flushright}
   }
 }
\cs_generate_variant:Nn \vincent_alignment:n {e}
\ExplSyntaxOff

\begin{document}
\begin{withalignment}{left}
\lipsum[1][1-4]
\end{withalignment}

\begin{withalignment}{center}
\lipsum[1][1-4]
\end{withalignment}

\newcommand{\myalignment}{center}
\begin{withalignment}{\myalignment}
\lipsum[1][1-4]
\end{withalignment}

\renewcommand{\myalignment}{right}
\begin{withalignment}{\myalignment}
\lipsum[1][1-4]
\end{withalignment}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .