我想创建一个接收一个参数和一个可选参数的环境,这样第一个参数将是标题,而可选参数将决定标题是左对齐还是居中。没有任何包可以实现吗?
答案1
可选参数可以是l
或c
(默认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}
答案2
没有任何包:使用经过检查的可选参数\pdfstrcmp
(pdftex 原语)。
如果#1
等于left
,则标题左对齐,否则居中。
\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}
答案3
编写环境以采用键值可选参数
\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}