3

I'm writing a description of a method, where the title of each section is formatted like Step X: <step title>. Now, I want to reference the section as Step X ("<step title>"). I am aware of \nameref, but I don't know how to remove the first two words of a string.

When trying to use stringstrings to remove the first two words, I get

Use of \removeword doesn't match its definition.

Minimum example:

\documentclass{article}
\usepackage{stringstrings}

\newcounter{step}

\begin{document}

\removeword\removeword{x1 x2 x3}

\end{document}

What can be done to have stringstrings remove the first two words?

The intended use (updated answer)

(If I should put that as separate question, I'll do)

I'm writing a description of a method, where the title of each section is formatted like Step X: <step title>. Now, I want to reference the section as Step X ("<step title>"). Currently, I'm using the solution https://tex.stackexchange.com/a/73710/9075, which suggests to redefine @currentlabelname. However, I have to type my section name twice.

Minimum example:

\documentclass{article}
\usepackage{csquotes}
\usepackage{hyperref}

\newcounter{step}

\newcommand{\titleinquotes}[1]{(\enquote{\nameref{sec:#1}})}

\begin{document}

We will see something in Step~\ref{S1} \titleinquotes{S1}.

\refstepcounter{step}
\label{S1}
\section{Step \thestep: Setup Environment}
\label{sec:S1}

\end{document}

The question is how `\titleinquotes' has to be changed to have following output

We will see something in Step 1 (“Setup Environment”)

The quick hack

\newcommand{\removedoubleword}[1]{\removeword[e]{#1} \removeword{\thestring}} 
\newcommand{\titleinquotes}[1]{(\enquote{\removedoubleword{\nameref{sec:#1}}})}

does not work: Use of \\removeword doesn't match its definition.. Without using \nameref, it works.

  • The example you show is not an example of what you really want. Can you add some more context? Removing a word from an explicitly given string is quite different from removing a word from something obtained with \nameref. – egreg Nov 14 '13 at 10:57
  • Also, it is not clear why you want to use stringstrings: if it is because you want to strip the "Step X:" part to reformat the title then why not take a more structured approach such as storing the step title as \formatstep{X}{title} and redefine \formatstep according to the two formats when using it in the two different contexts (section title and reference)? If you are interested in this approach I could attempt an answer. – Bordaigorl Nov 14 '13 at 11:38
6

According to the stringstring documentation, you cannot directly nest its commands. Instead, stringstring manipulation commands provide a specific mechanism for composability: They store the result in \thestring, which can be used as input to subsequent commands. So you need two steps.

Most commands also "print out" the resulting \thestring. This can be supressed by passing the [q] (quiet) or [e] (encoded) option. The latter makes sure that special characters (such as \$) do not get expanded, which increases the robustness if \thestring is fed into another stringstring command.

\documentclass{article}
\usepackage{stringstrings}

\newcounter{step}

\begin{document}

  \removeword[e]{x1 x2 x3}
  \removeword{\thestring}

\end{document}
  • 1
    Instead of [q] you could also use the encode mode [e]. For convenience I would deine the follwoing command \newcommand{\removedoubleword}[1]{\removeword[e]{#1} \removeword{\thestring}} – crixstox Nov 14 '13 at 10:45
  • @crixtox: I was just reading about encoded mode, which certainly should be used here. I will edit the answer. – Daniel Nov 14 '13 at 10:49
  • 1
    I couldn't have said it better myself. [package author] – Steven B. Segletes Nov 14 '13 at 11:00

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.