Ad question 1:
I think the classic way of handling the boolean would be using boolean keys as provided by packages like xkeyval or pgfkeys or by expl3.
If you don't like to use such packages/interfaces, you can define your own mechanism for cranking out by means of delimited arguments whether the value "true/yes" or the value "false/no" or something else is provided and setting an \if⟨name of if-switch⟩
-switch accordingly by applying one of the commands \⟨name of if-switch⟩true
/\⟨name of if-switch⟩false
:
\documentclass{article}
\usepackage{keyval}
\makeatletter
%
%----------------------------------------------------------------------------------------------
%
% \truefalsefork{<value>}%
% {<tokens if <value>=true/yes>}%
% {<tokens if <value>=false/no>}%
% {<tokens> if <value> neither is true/yes nor is false/no}
%
\@ifdefinable\gobbletorelax{\long\def\gobbletorelax#1\relax{}}%
\newcommand\truefalsefork[1]{%
\lowercase{\@truefalsefork{#1}}%
}%
\newcommand\@truefalsefork[4]{%
\ifcat$\detokenize\expandafter{\gobbletorelax#1\relax}$%
\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{%
\selecttruefalsecase
\relax#1\relax yes\relax false\relax no\relax{#2}%
\relax true\relax#1\relax false\relax no\relax{#2}%
\relax true\relax yes\relax #1\relax no\relax{#3}%
\relax true\relax yes\relax false\relax#1\relax{#3}%
\relax true\relax yes\relax false\relax no\relax{#4}\relax\relax\relax\relax
}{#4}%
}%
\@ifdefinable\selecttruefalsecase{%
\long\def\selecttruefalsecase#1\relax true\relax yes\relax false\relax no\relax#2#3\relax\relax\relax\relax{#2}%
}%
%
%----------------------------------------------------------------------------------------------
%
% Infrastructure for boolean-key hideInToc
%
\newcommand\hideInTocErrordefault[1]{%
\PackageError{sectionLvl}{%
Macro \string\sectionLvl:\MessageBreak
Invalid value '\detokenize{#1}'\MessageBreak
for key hideInToC\on@line.\MessageBreak
Value "true" is assumed%
}{%
Only values true/false/yes/no are allowed!\MessageBreak
(Lettercasing doesn't matter.)\MessageBreak
Value "true" is assumed.%
}%
\hideInToctrue
}%
\newif\ifhideInToc
\define@key{mosquito}{hideInToC}[true]{%
\truefalsefork{#1}{\hideInToctrue}{\hideInTocfalse}{\hideInTocErrordefault{#1}}%
}%
%
%----------------------------------------------------------------------------------------------
%
\newcommand\sectionLvl[3][]{%
\setkeys{mosquito}{hideInToC=false, #1}%
\ifcase#2\relax
\expandafter\chapter
\or
%section
\ifhideInToc\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{\expandafter\section\expandafter*}{\expandafter\section}%
\else
\expandafter\subsection
\fi
{#3}%
}%
\makeatother
\begin{document}
\tableofcontents
%++++++++++ after ToC********
\sectionLvl{1}{title}
\sectionLvl[hideInToC=TrUe]{1}{title1}
% Check the error-message-mechanism by uncommenting the next line:
% \sectionLvl[hideInToC=WeiRd]{1}{title1}
\sectionLvl{1}{title2}
\sectionLvl{2}{title2-1}
\end{document}
Be aware that this is sort of a poor-man's implementation of a boolean-key.
The packages mentioned above provide more sophisticated routines.
Ad question 2:
When looking at the code presented by you, I see the following:
As the \ifx\mosquito@hideInToC\@empty..\else..\fi
-thingie is nested within the last \or
-branch of an \ifcase..\or..\else..\fi
-thingie, both within the "true"-branch and within the "false"-branch of the \ifx\mosquito@hideInToC\@empty..\else..\fi
-thingie you have taken measures for ensuring that an \expandafter
-chain is present which triggers expansion of the subsequent \else
of the surrounding \ifcase..\or..\else..\fi
-thingie which in turn leads to the removal of the entire "surrounding" "else"-branch and the \fi
before \section
/\section*
gets carried out. Thus due to these \expandafter
-chains both \section
from the \ifx\mosquito@hideInToC\@empty..\else..\fi
-thingie's "false"-branch and \section*
from the \ifx\mosquito@hideInToC\@empty..\else..\fi
-thingie's "true"-branch can fetch {#3}
as its argument as no other tokens stemming from the surrounding \ifcase..
-expression are in the way.
If you just replace the \ifx\mosquito@hideInToC\@empty..\else..\fi
-thingie by something like the sequence \ifx\mosquito@hideInToC\@empty\expandafter\@firstoftwo\fi\section*
, then such an \expandafter
-chain is not there. Thus the tokens \else..\fi
from the surrounding \ifcase..\or..\else..\fi
-thingie are still there when the argument of \section
/\section*
is gathered. Thus not the sequence {#3}
but the token \else
will be the argument of \section
/\section*
, leading to unexpected results/unexpected behavior.
You could define an additional macro \@twooftwo
and do:
\documentclass{article}
\usepackage{keyval}
\makeatletter
\define@key{mosquito}{hideInToC}[false]{\def\mosquito@hideInToC{#1}}
\newcommand\@twooftwo[2]{#1#2}
\newcommand\sectionLvl[3][]{%
\setkeys{mosquito}{hideInToC={},#1}%
\ifcase#2\relax
\expandafter\chapter
\or
%section
\ifx\mosquito@hideInToC\@empty\expandafter\@firstoftwo\else\expandafter\@twooftwo\fi
{\expandafter\section}{\expandafter*}%
\else
\expandafter\subsection
\fi
{#3}%
}%
\makeatother
\begin{document}
\tableofcontents
++++++++++ after ToC********
\sectionLvl{1}{title}
\sectionLvl[hideInToC]{1}{title1}
\sectionLvl{1}{title2}
\sectionLvl{2}{title2-1}
\end{document}
You could also do the "standard" \if..\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
-thingie:
\documentclass{article}
\usepackage{keyval}
\makeatletter
\define@key{mosquito}{hideInToC}[false]{\def\mosquito@hideInToC{#1}}
\newcommand\sectionLvl[3][]{%
\setkeys{mosquito}{hideInToC={},#1}%
\ifcase#2\relax
\expandafter\chapter
\or
%section
\ifx\mosquito@hideInToC\@empty\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{\expandafter\section}{\expandafter\section\expandafter*}%
\else
\expandafter\subsection
\fi
{#3}%
}%
\makeatother
\begin{document}
\tableofcontents
++++++++++ after ToC********
\sectionLvl{1}{title}
\sectionLvl[hideInToC]{1}{title1}
\sectionLvl{1}{title2}
\sectionLvl{2}{title2-1}
\end{document}
But these are just examples exhibiting how to get the \expandafter
-tokens in line for removing the \ifcase..\or..\else..\fi
-thingie's \else..\fi
before carrying out \section
/\section*
and hereby having TeX gather \section
/\section*
's argument.
I don't recommend this because you don't have infrastructure for handling the situation of the user providing an inappropriate value for the key hideInToC
.