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:

 {\begin{\csname #1@somethingtitle\endcsname}
  \bfseries #2
  \end{\csname #1@somethingtitle\endcsname}}

Thanks in advance.

I followed the suggestion of Teepeemm by adding the following

  • 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

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

enter image description here



  \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}




Here is something without any optional argument.

Here is something with a single optional argument.

Here is something with two optional arguments.


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.