3

I am trying to check the definition of a command (either arabic, roman, Roman, Alph etc.) and from this information change the formatting of section titles, while making use of the command definition in the formatting commands.

The problem I am having is that I don't seem able to use \ifthenelse to check if two things are equal if there is a backslash present in the equality. However, going down another route, I can't use a backslash before the command to produce a new command since two backslashes yield a linebreak (this is shown in the example below).

Note that changing \\sectionnumstyle to \arabic in the first instance and then to \Alph in the second instance in the \titleformat command will produce a desired output.

\documentclass[10pt,a5paper]{book}
\usepackage{ifthen}
\usepackage[calcwidth]{titlesec}

\newcommand\sectionnumstyle{arabic}

\titleformat{\section}[frame]
    {\centering\Large}
    {Section \ifthenelse{\equal{\sectionnumstyle}{arabic}}
        {\oldstylenums{\\sectionnumstyle{section}}}
        {\\sectionnumstyle{section}}}
    {1em}{\centering}[]

\begin{document}
\section{Hello}

\renewcommand\sectionnumstyle{Alph}

\section{Goodbye}
\end{document}

I understand that I could simply use \ifthenelse, checking each possible value (arabic, roman, Roman, Alph etc.) and produce an outcome of \oldstylenums\arabic{section}, \roman{section}, \Roman{section}, \Alph{section} etc. based on each individual possible definition of the command however this seems long and unnecessary. If anyone has a simpler and faster solution it would be much appreciated.

4

You need to learn about \csname...\endcsname. For example, \csname\sectionnumstyle\endcsname accomplishes what I think you were trying to accomplish, unsuccessfully, with \\sectionnumstyle.

\documentclass[10pt,a5paper]{book}
\usepackage{ifthen}
\usepackage[calcwidth]{titlesec}

\newcommand\sectionnumstyle{arabic}

\titleformat{\section}[frame]
    {\centering\Large}
    {Section \ifthenelse{\equal{\sectionnumstyle}{arabic}}
        {\oldstylenums{\csname\sectionnumstyle\endcsname{section}}}
        {\csname\sectionnumstyle\endcsname{section}}}
    {1em}{\centering}[]

\begin{document}
\section{Hello}

\renewcommand\sectionnumstyle{Alph}

\section{Goodbye}
\end{document}

enter image description here

1
  • You're a beautiful man
    – Eddie
    May 14 '18 at 14:01

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.