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?
3 Answers
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}}
{\par\addvspace{\topsep}}
\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}
-
-
-
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?– JoanFeb 21, 2019 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...– JoanFeb 1, 2019 at 18:01 -
@Joan: You need an empty line after
\centering
– user31729Feb 1, 2019 at 18:04 -
-
@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.– user31729Feb 1, 2019 at 18:25
-
1@Joan: See the answers to the question here -- it takes too long to explain this in a comment– user31729Feb 1, 2019 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
\define@choicekey*{paralign}{align}[\val\nr]{left,center,right}[center]{% Add align=? key-value
\gdef\titlealign{}%
\ifcase\nr\relax
\gdef\titlealign{\raggedright}\or% 0 = left
\gdef\titlealign{\centering}\or% 1 = center
\gdef\titlealign{\raggedleft}% 2 = right
\fi
}
\define@cmdkey{paralign}{format}[]{}% Add format=? key-value
\newenvironment{mytitle}[2][]{%
\par\addvspace{\medskipamount}
\begingroup
\setkeys{paralign}{%
align,format,% Default settings (align=center,format={})
#1}% Local settings
\titlealign \cmdKV@paralign@format #2%
\par
\endgroup
\nobreak\smallskip
}{%
\par\addvspace{\medskipamount}
}
\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}
mdframed
ortcolorbox
, with lot of additional features.xparse
package to do that.