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
Joan
  • 479
  • 1
    Perhaps this would be easier with the \NewDocumentEnvironment command from xparse. – Bernard Feb 21 '19 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 '19 at 22:58
  • There is also \@ifnextchar[, if you want to show off. – John Kormylo Feb 21 '19 at 23:01
  • 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 '19 at 23:05
  • Perhaps you can start by telling us why the linked post is not properly solving your current setup? – Werner Feb 21 '19 at 23:31
  • @Werner - the linked post solves my initial question, but then the need arose to use two optional arguments and what I want is to reuse the proposed code in the solution of the linked post... – Joan Feb 21 '19 at 23:37
  • @Joan: Two optional arguments is odd to manage, since you can only have the second if you have the first, making the first semi-optional. Hence my suggestion to use a key-value approach: \begin{something}[align=center,format=\bfseries]{title} (say) – Werner Feb 21 '19 at 23:41
  • @Werner - Could I use key-value without using packages? – Joan Feb 22 '19 at 00:17
  • @Joan: Sure, just copy all the code from the package into your preamble. What's the issue with using a package? You're probably doing that already, aren't you? For example, the article document class is just a selection of macros that set up the document profile. Packages extend it to be able to make all kinds of nifty things, like managing key-value optional argument... – Werner Feb 22 '19 at 00:26
  • @Werner - I know it's annoying to request a solution without the help of packages, because it's clear that the packages have been made to make life easier for users, but the work I'm doing requires doing most of the things without using these packages, I hope you can understand it without being one of those people who get upset about this. – Joan Feb 22 '19 at 00:32

1 Answers1

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.

Werner
  • 603,163