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?
Asked
Active
Viewed 1,292 times
4
3 Answers
5
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 01 '19 at 18:01 -
-
-
@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. – Feb 01 '19 at 18:25
-
I have add
\begingroup \endgroupto definition of\footitleformat. Thanks @Christian Hupfer – Joan Feb 01 '19 at 19:12 -
I have a problem... I have add the code to myclass.cls file, but don't work. Only work if I add the code a .tex file. – Joan Feb 01 '19 at 19:44
-
@Joan: I cannot solve that problem because I have no idea what your
myclass.clsdoes. And it's another question. – Feb 01 '19 at 19:50 -
My class is based on
book.cls. I just added your code to this file, but don't work. – Joan Feb 01 '19 at 19:54 -
@Joan: Again: I can't help you: My code does nothing special -- it uses standard TeX/LaTeX macros from the LaTeX core! There is nothing that should interfere with
book.clsetc. You must be doing something wrong in your class. Writing a class requires some deeper knowledge of LaTeX etc. – Feb 01 '19 at 19:56 -
I'm sorry, it was my fault, I accidentally "deleted" the empty line...I would like to know why this empty line is necessary. – Joan Feb 01 '19 at 20:16
-
1@Joan: See the answers to the question here -- it takes too long to explain this in a comment – Feb 01 '19 at 20:25
5
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}
egreg
- 1,121,712
-
-
-
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 '19 at 21:04
3
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}
Werner
- 603,163



mdframedortcolorbox, with lot of additional features. – Feb 01 '19 at 17:25xparsepackage to do that. – Bernard Feb 01 '19 at 17:26