Is it possible to use the xstring
package to pass the result of one macro into the definition of another macro?
If it is not possible, is there another way to accomplish this with LaTeX?
Specifically, why does this minimal working example work:
\documentclass{article} \usepackage{mathrsfs} \usepackage{xstring}
\newenvironment{axioms}[2]{
\StrMid{#1}{0}{1}[\firstletter]
\firstletter
\newcommand{\axiom}[1]{ \item[##1] }
#2 -- the \textbf{#1 Axioms}
\begin{itemize} }{\end{itemize}}
\title{} \author{} \date{} \begin{document}
\begin{axioms}{Matroid}{$\mathscr{M}$}
\axiom{1}
\end{axioms}
\end{document}
and why does this minimal non-working example not work? (The difference is in the \newcommand
line.)
\documentclass{article} \usepackage{mathrsfs \usepackage{xstring}
\newenvironment{axioms}[2]{
\StrMid{#1}{0}{1}[\firstletter]
\newcommand{\axiom}[1]{ \firstletter \item[##1] }
#2 -- the \textbf{#1 Axioms}
\begin{itemize} }{\end{itemize}}
\title{} \author{} \date{} \begin{document}
\begin{axioms}{Matroid}{$\mathscr{M}$}
\axiom{1}
\end{axioms}
\end{document}
It occurred to me that there is usually a lot of redundancy in my LaTeX code, and that I hold myself to much lower quality standards than with other languages. In order to begin changing this I have tried to use more (nested) macros, but with mixed success.
Page 13 of the documentation for xstring
says:
The macros of this package are not purely expandable, i.e. they cannot be put in the argument of an \edef. Nestling macros is not possible neither. For this reason, all the macros returning a result (i.e. all excepted the tests) have an optional argument in last position. The syntax is [〈name〉], where 〈name〉 is the name of the control sequence that will receive the result of the macro: the assignment is made with an \edef which make the result of the macro 〈name〉 purely expandable.
Keeping this in mind, I tried to use the result of an xstring
command as part of the definition of a \newcommand
within a \newenvironment
by using the [\result]
thing mentioned in the documentation. However, I could not make this work, and got the error:
Something's wrong--perhaps a missing \item. \axiom{1}
I am also looking at pp. 17-19 of the same documentation for a possible solution, although right now I don't understand them enough. If I can decipher them and figure out something useful I will try to remember to post something.
If one of these questions (1)(2)(3)(4) answers my question, can you please explain in a little more detail how? I have read them but don't understand how to apply their answers to my situation. Specifically I want to capture only the first letter of an input and then use that first letter as an argument for another macro, not to create another macro.