1

I'm trying to define a newcommand in a .cls file as follows:

\newcommand{\test}[2][]{%
    \ifthenelse{\isempty{#1}}%
    {\raggedright\itshape{#2}}%
    {\raggedright#1 \hfill \itshape{#2}}%
}

However, this gives LaTeX Error: Something's wrong--perhaps a missing \item. I have included the package xifthen with \RequirePackage{xifthen}. I should also mention that if one removes the [], which is intended for the optional argument, the code seems to work just fine. What am I missing here?

EDIT: here's the main part of the class file:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{my_class}[2020/07/31 My custom class]
\LoadClass{article}
\RequirePackage[T1]{fontenc}
\RequirePackage{xifthen}

\newenvironment{myenv}%
    {\addvspace{0.1in}\noindent\ignorespaces}
    {\par\noindent%\addvspace{0.1in}
    \ignorespacesafterend}

\newcommand{\first}[2]{%
    {\begingroup \raggedright \textbf{#1} \hfill \textit{#2} \newline \endgroup}%
}

\newcommand{\second}[2][]{%
    \ifthenelse{\isempty{#1}}%
    {\textit{\raggedleft #2} \newline}%
    {\begingroup \raggedright #1 \hfill \textit{#2} \newline \endgroup}%
}

and here's a minimal working example:

\documentclass{my_class}

\begin{document}

\section{First section}

\begin{myenv}
    \first{This is bold text}{This is italic text}
    \second[Optional argument given]{This is italic text}
\end{myenv}

\begin{myenv}
    \first{Bold text}{Italic text}
    \second{Optional argument omitted}
\end{myenv}

\end{document}

When \second is provided with the optional argument, it seems to work fine, outputting the first argument to the left and the second to the right of the same line. However, when the optional argument is not provided, the second argument is written as if were left-justified, not right-justified (which is what I want). I used groups to limit the the effects of the switches such as \raggedright but I don't think I did it right.

10
  • I am not very familiar with this approach. I do prefer xparse when defining commands with optional arguments, because it provides more tools for customization. (See this document for more info.)
    – Alan Xiang
    Aug 2 '20 at 20:21
  • 1
    Maybe an incompatibility with some other part of your .cls file? This code, inserted as an ordinay macro in a preamble, works fine.
    – Bernard
    Aug 2 '20 at 21:00
  • 1
    This won't produce the error, by itself. Probably the context in which you use it is the problem. However this is not particularly good: using the command will continue ragged right and italic type forever.
    – egreg
    Aug 2 '20 at 21:02
  • To expand on what @egreg said: this isn’t the source of your error, but you probably meant \textit{\raggedright {#1}} instead of \raggedright\itshape.
    – Davislor
    Aug 3 '20 at 1:06
  • 1
    To solve this problem, we would need a MWE that reproduces the bug. It’s in some part of the code that you didn’t post.
    – Davislor
    Aug 3 '20 at 1:07
1

I'm not sure what's the aim of the code, but there are several things that are superfluous.

\documentclass{article}

\newenvironment{myenv}
    {\par\addvspace{0.1in}}
    {\par\addvspace{0.1in}}
\newcommand{\first}[2]{%
    {\raggedright\textbf{#1}\hfill\textit{#2}\par}%
}
\newcommand{\second}[2][]{%
    {\raggedright#1\hfill\textit{#2}\par}%
}

\begin{document}

\section{First section}

\begin{myenv}
    \first{This is bold text}{This is italic text}
    \second[Optional argument given]{This is italic text}
\end{myenv}

\begin{myenv}
    \first{Bold text}{Italic text}
    \second{Optional argument omitted}
\end{myenv}

\end{document}

enter image description here

Note that \newline is out of place and would produce warnings. However, your code doesn't show the mentioned error in my experiment.

1
  • This works perfectly, thanks!
    – fresh
    Aug 3 '20 at 13:50

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.