# Environment with argument and optional argument

I want to create an environment that receives an argument and an optional argument in such a way that the first argument will be a title and the optional argument will decide whether the title should be justified to the left or centered. It's possible without any package?

• Yes, this is possible and there are many packages, that provide such environments already, e.g. mdframed or tcolorbox, with lot of additional features. – user31729 Feb 1 at 17:25
• You should take look at the xparsepackage to do that. – Bernard Feb 1 at 17:26
• I want to do it without any package. – Joan Feb 1 at 17:29

The optional argument can be l or c (default c).

\documentclass{article}

\makeatletter
\newenvironment{something}[2][c]
{\begin{\csname #1@somethingtitle\endcsname}
\bfseries #2
\end{\csname #1@somethingtitle\endcsname}}
\newcommand\l@somethingtitle{flushleft}
\newcommand\c@somethingtitle{center}
\makeatother

\begin{document}

\begin{something}{This is centered}
This is the environment's contents
This is the environment's contents
This is the environment's contents
This is the environment's contents
\end{something}

\begin{something}[l]{This is left flush}
This is the environment's contents
This is the environment's contents
This is the environment's contents
This is the environment's contents
\end{something}

\end{document}


• I would like to know why you use the char @? – Joan Feb 1 at 22:10
• @Joan In order to avoid clashes with user macros. – egreg Feb 1 at 23:02
• in the case that I want to add an optional argument that allows to decide if the title wants to be added to the ToC, how could I do it? – Joan Feb 21 at 21:04

Without any package: Using an optional argument that is checked with \pdfstrcmp (a pdftex primitive).

If #1 is equal to left, the title is left aligned, in any other case it is centered.

\documentclass{article}

\newenvironment{foo}[2][]{%
\ifnum\pdfstrcmp{#1}{left}=0%
#2%
\else
\hfil#2\hfil%
\fi%
\medskip

}{}

\newcommand{\footitleformat}[1]{%
\bfseries\MakeUppercase{#1}%
}

\newenvironment{fooother}[2][]{%
\ifnum\pdfstrcmp{#1}{left}=0%
\footitleformat{#2}%
\else
\begingroup
\centering
\footitleformat{#2}%

\endgroup
\fi%
\medskip

}{}

\begin{document}

\begin{foo}{This is centered}
Foo stuff
\end{foo}

\begin{foo}[left]{This is not centered}
Other Foo stuff
\end{foo}

\begin{fooother}{This is centered again}

Yet another Foo stuff
\end{fooother}

\end{document}

\documentclass{article}

\newenvironment{foo}[2][]{%
\ifnum\pdfstrcmp{#1}{left}=0%
#2%
\else
\hfil#2\hfil%
\fi%
\medskip

}{}

\begin{document}

\begin{foo}{This is centered}
Foo stuff
\end{foo}

\begin{foo}[left]{This is not centered}
Other  Foo stuff
\end{foo}

\end{document}

• I try to use {\centering\bfseries\MakeUppercase{#2}} but the text is not center... – Joan Feb 1 at 18:01
• @Joan: You need an empty line after \centering – user31729 Feb 1 at 18:04
• Don't understand. – Joan Feb 1 at 18:13
• @Joan: I changed the answer: I was too short in my explanation ... you need an empty line after the text that is to be centered. – user31729 Feb 1 at 18:25
• @Joan: See the answers to the question here -- it takes too long to explain this in a comment – user31729 Feb 1 at 20:25

Write the environment to take a key-valued optional argument

\documentclass{article}

\usepackage{xkeyval}

\makeatletter
% From the xkeyval documentation (section 3.3 Choice keys)
% http://texdoc.net/texmf-dist/doc/latex/xkeyval/xkeyval.pdf
\gdef\titlealign{}%
\ifcase\nr\relax
\gdef\titlealign{\raggedright}\or% 0 = left
\gdef\titlealign{\centering}\or% 1 = center
\gdef\titlealign{\raggedleft}% 2 = right
\fi
}

\newenvironment{mytitle}[2][]{%
\begingroup
\setkeys{paralign}{%
align,format,% Default settings (align=center,format={})
#1}% Local settings
\titlealign \cmdKV@paralign@format #2%
\par
\endgroup
\nobreak\smallskip
}{%
}
\makeatother

\begin{document}

\begin{mytitle}{Centred titled}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus placerat
enim ligula, a efficitur augue sodales non\ldots
\end{mytitle}

\begin{mytitle}[align=left]{Left-aligned title}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus placerat
enim ligula, a efficitur augue sodales non\ldots
\end{mytitle}

\begin{mytitle}[align=right,format=\bfseries]{Right-aligned title}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus placerat
enim ligula, a efficitur augue sodales non\ldots
\end{mytitle}

\end{document}