How to replace characters from jobname?
Apart from expl3
, you can also use StrSubstitute
with the option to save the result in a macro. Note that you need to convert _
to the right catcode, for example using \string
.
MWE:
\documentclass{article}
\usepackage{xstring}
\title{\textbf{\subfinal}}
\author{}
\date{}
\begin{document}
\StrSubstitute{\jobname}{-}{: }[\subsemi]
\StrSubstitute{\subsemi}{\string_}{ }[\subfinal]
\maketitle
The name of the tex file is {\tt{Chap-Name\_of\_Section.tex}}. I am running {\verb|pdflatex|} on the tex file. After using {\verb|\jobname|} I get the title.
I want to print the title as ``Chap: Name of Section''. Note that `-' sign is to be replaced by `:' followed by a single space and underscores (`\_') are to be replaced by spaces.
\end{document}
Result:
Same idea as in @jakun's answer, but implemented (without packages, also) in a way which is easier (more convenient, rather, as it takes less space) to extend to more special characters. Expandable, too.
\documentclass{article}
\long\def\DoThis #1#2\OrThat #3{\fi #1}
\makeatletter
\let\OrThat\@firstofone
\makeatother
\begingroup
\makeatletter
\catcode`_=12 % other
% if using Babel with a language that makes : a shorthand (French, e.g.)
% then you need \catcode`: \active here and \DoThis{\noexpand: }
\def\myParser#1{%
\ifx\relax#1\DoThis{\@gobble}\fi
\ifx -#1\DoThis{: }\fi
\ifx _#1\DoThis{ }\fi
\OrThat{#1}\myParser
}%
\xdef\parsedJobname{\expandafter\myParser\jobname\relax}
\endgroup
\title{\parsedJobname}
\begin{document}
\maketitle
The parsed jobname is ``\parsedJobname''.
\end{document}
If the above is file Test_Titre-Un.tex
then the result is
Code for French as per code comment.
\begingroup
\makeatletter
\catcode`_=12 % other
\catcode`\: \active
\def\myParser#1{%
\ifx\relax#1\DoThis{\@gobble}\fi
\ifx -#1\DoThis{\noexpand: }\fi
\ifx _#1\DoThis{ }\fi
\OrThat{#1}\myParser
}%
\xdef\parsedJobname{\expandafter\myParser\jobname\relax}
\endgroup
Produces (using babel+french
):
Here is more simple, format independent, expandable solution:
\def\cnvjobname{\expandafter\cnvjobnameA\jobname\relax}
\def\cnvjobnameA#1{\ifx\relax#1\else
\ifx-#1: \else \expandafter\ifx\string_#1 \else #1\fi\fi
\expandafter\cnvjobnameA\fi
}
%test:
\message{... "\cnvjobname"}
\bye