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.
\textit{\raggedright {#1}}
instead of\raggedright\itshape
.