如何替换职位名称中的字符?

如何替换职位名称中的字符?

我有一个tex文件,上面有名称Chap-Name_of_Section.tex,我正在运行pdflatex它。我想根据文件名自动设置文档的标题。这可以使用 来完成\jobname

这是我的 MWE。将此文件另存为Chap-Name_of_Section.tex

\documentclass[12pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{a4wide}

\title{\textbf{\jobname}}
\author{}
\date{}

\begin{document}
\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}

但这不是我想要的。我想将标题打印为Chap: Name of Section。请注意,-符号应替换为,:后跟一个空格,下划线_应替换为空格。

\StrSubstitute我从包中找到xstring。但这只能更改一个字符。例如,如果我在代码中替换\jobname\StrSubstitute[0]{\jobname}{-}{: },标题将是Chap: Name_of_Section

如何相应地替换-_

答案1

除了expl3,您还可以使用StrSubstitute选项将结果保存在宏中。请注意,您需要转换_为正确的 catcode,例如使用\string

梅威瑟:

\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}

结果:

在此处输入图片描述

答案2

与@jakun 的想法相同回答,但实现方式(也无需软件包)更容易(更方便,因为它占用的空间更少)扩展到更多特殊字符。同样可扩展。

\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}

如果以上是文件Test_Titre-Un.tex,则结果是

在此处输入图片描述

按照代码注释对法语进行编码。

\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

生成(使用babel+french):

在此处输入图片描述

答案3

这里有更简单、格式独立、可扩展的解决方案:

\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

答案4

您可以使用expl3优秀的正则表达式模块:

\documentclass[12pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{a4wide}
\usepackage{expl3}

\ExplSyntaxOn
\tl_set:NV \l_tmpa_tl \c_sys_jobname_str
\regex_replace_once:nnN { - } { \cO:\  } \l_tmpa_tl
\regex_replace_all:nnN { _ } { \  } \l_tmpa_tl
\exp_args:NV \title \l_tmpa_tl
\ExplSyntaxOff

\begin{document}
\maketitle
\end{document}

相关内容