0

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 two optional argument will decide whether the title should be justified to the left or centered plus if the title wants to be added to the ToC.

I have this code with one optional argument (thanks to @egreg), so I like to modify for the case of two optional argument:

\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

Thanks in advance.


I followed the suggestion of Teepeemm by adding the following

\if\detokenize{C}\detokenize{#1}\relax
 \addcontentsline{toc}{chapter}{#2}
\fi
\if\detokenize{L}\detokenize{#1}\relax
 \addcontentsline{toc}{chapter}{#2}
\fi
  • 1
    Perhaps this would be easier with the \NewDocumentEnvironment command from xparse. – Bernard Feb 21 at 22:43
  • 1
    The basic LaTeX syntax works well if you have one optional argument that comes first. If you diverge from that, it gets much more complicated. It might be easier to have one optional argument that combines the two possibilities: c, C, l, and L, where capital letters mean that it should be added to the ToC. – Teepeemm Feb 21 at 22:58
  • There is also \@ifnextchar[, if you want to show off. – John Kormylo Feb 21 at 23:01
  • Possible duplicate: Environment with argument and optional argument – Werner Feb 21 at 23:03
  • I'd suggest you use my approach in the linked answer; it uses xkeyval that can take multiple key-values in the optional argument. – Werner Feb 21 at 23:05
2

Here is an option that provide two optional arguments and a mandatory title:

enter image description here

\documentclass{article}

\makeatletter

\newcommand{\something@aux@A}[1][c]{%
  \def\something@halign{#1}%
  \something@aux@B%
}
\newcommand{\something@aux@B}[2][y]{%
  \expandafter\begin\expandafter{\csname\something@halign @somethingtitle\endcsname}
    \csname phantomsection\endcsname% If you're using hyperref
    \bfseries #2
  \expandafter\end\expandafter{\csname\something@halign @somethingtitle\endcsname}
  \def\something@toc{#1}
  \ifx\something@toc\something@toc@y
    \addcontentsline{toc}{section}{#2}%
  \fi
  \par\addvspace{\topsep}
}
\newenvironment{something}
 {\something@aux@A}
 {}
\newcommand{\l@somethingtitle}{flushleft}
\newcommand{\c@somethingtitle}{center}
\newcommand{\r@somethingtitle}{flushright}
\def\something@toc@y{y}

\makeatother

\begin{document}

\tableofcontents

\begin{something}{titleA}
Here is something without any optional argument.
\end{something}

\begin{something}[l]{titleB}
Here is something with a single optional argument.
\end{something}

\begin{something}[r][n]{titleC}
Here is something with two optional arguments.
\end{something}

\end{document}

The key is to use auxiliary macros to capture the arguments and then store them for use elsewhere.

The environment something's first optional argument specifies the horizontal alignment. The second specifies whether or not the entry should be in the ToC, followed by the mandatory title.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.