Here's with expl3
; you can use l
, c
or r
besides left
, center
or right
.
\documentclass{article}
\usepackage{lipsum} % for mock text
\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
\begin{document}
\section{Long arguments}
\begin{withalignment}{left}
\lipsum[1][1-4]
\end{withalignment}
\begin{withalignment}{center}
\lipsum[1][1-4]
\end{withalignment}
\begin{withalignment}{right}
\lipsum[1][1-4]
\end{withalignment}
\section{Short arguments}
\begin{withalignment}{l}
\lipsum[1][1-4]
\end{withalignment}
\begin{withalignment}{c}
\lipsum[1][1-4]
\end{withalignment}
\begin{withalignment}{r}
\lipsum[1][1-4]
\end{withalignment}
\end{document}

With error management:
\documentclass{article}
\usepackage{lipsum} % for mock text
\ExplSyntaxOn
\NewDocumentEnvironment{withalignment}{m}
{
\cs_if_exist:cTF { \vincent_alignment:n { #1 } }
{% the environment exists
\begin{\vincent_alignment:n { #1 }}
}
{
\msg_error:nnn { vincent/alignment } { bad-argument } { #1 }
}
}
{
\cs_if_exist:cT { \vincent_alignment:n { #1 } }
{
\end{\vincent_alignment:n { #1 }}
}
}
\msg_new:nnnn { vincent/alignment } { bad-argument }
{
Wrong~argument~'#1'~to~'withalignment'
}
{
The~allowed~arguments~are~l,~c,~r,~left,~center,~right
}
\cs_new:Nn \vincent_alignment:n
{
\str_case:nn { #1 }
{
{c}{center}
{center}{center}
{l}{flushleft}
{left}{flushleft}
{r}{flushright}
{right}{flushright}
}
}
\ExplSyntaxOff
\begin{document}
\begin{withalignment}{xyzzz}
\end{withalignment}
\section{Long arguments}
\begin{withalignment}{left}
\lipsum[1][1-4]
\end{withalignment}
\begin{withalignment}{center}
\lipsum[1][1-4]
\end{withalignment}
\begin{withalignment}{right}
\lipsum[1][1-4]
\end{withalignment}
\section{Short arguments}
\begin{withalignment}{l}
\lipsum[1][1-4]
\end{withalignment}
\begin{withalignment}{c}
\lipsum[1][1-4]
\end{withalignment}
\begin{withalignment}{r}
\lipsum[1][1-4]
\end{withalignment}
\end{document}
The wrong argument would make TeX issue
! Package vincent/alignment Error: Wrong argument 'xyzzz' to 'withalignment'
For immediate help type H <return>.
...
l.48 \begin{withalignment}{xyzzz}
? h
The allowed arguments are l, c, r, left, center, right
\newenvironment
doesn't have access to the arguments#1
, …. You need to save them in a macro or use the new\NewDocumentEnvironment
fromxparse
. Instead of the ifthenelsing just use#1
directly as the environment name. For that, define aflushcenter
environment that's a copy ofcenter
. (I'd use a key-value system or justc
,r
andl
as arguments that can be\if
ed.)