Replace a character with a string in LaTeX

时光总嘲笑我的痴心妄想 提交于 2019-12-05 02:47:50

The general case is rather more tricky (when you're not using commas as separators), but the example you gave can be coded without too much trouble with some knowledge of the LaTeX internals.

\documentclass[12pt]{article}
\makeatletter
\newcommand\formatnice[1]{%
  \let\@formatsep\@formatsepinit
  \@for\@ii:=#1\do{%
    \@formatsep
    \formatentry{\@ii}%
  }%
}
\def\@formatsepinit{\let\@formatsep\formatsep}
\makeatother
\newcommand\formatsep{,}
\newcommand\formatentry[1]{#1}
\begin{document}
\formatnice{abc,def}

\renewcommand\formatsep{\,$\rightarrow$\,}
\renewcommand\formatentry[1]{\textbf{#1}}
\formatnice{abc,def}
\end{document}

it looks like your "spaces" problem is from a bug in that package. If you surround the "\GetTokens" macro with, say, commas, then you'll see that the extra space is inserted by that macro.

Yes there are bugs in tokenizer package. As I said on my blog, the bugfix is to use the following correcting code instead of just "\usepackage[trim]{tokenizer}":

\usepackage[trim]{tokenizer}  

\def\SH@GetTokens#1,#2\@empty{%
    \def\SH@token{#1}%
    \ifx\SH@trimtokens\SH@true% strip spaces if requested
        \TrimSpaces\SH@token%
    \fi%
    \SH@DefineCommand{\SH@FirstArgName}{\SH@token}%
    \SH@DefineCommand{\SH@SecondArgName}{#2}%
    }
\def\SH@CheckTokenSep#1,#2\@empty{%
    \def\SH@CTSArgTwo{#2}%
    \ifx\SH@CTSArgTwo\@empty%
        \edef\SH@TokenValid{\SH@false}%
    \else%
        \edef\SH@TokenValid{\SH@true}%
    \fi%
    }

I will report this bugfix to the developer Sascha Herpers

Brent.Longborough

There's a LaTeX package called tokenizer which may help you to do what you want.

Here's a hack (but pure LaTeX, no internals) which gets close to what I think you want, but with some extraneous spaces I haven't quite been able to fix. Perhaps Will Robertson can advise further? Unlike his slightly more polished answer, I haven't parameterised the bits and pieces, Here goes:

\usepackage{forloop}  
\usepackage[trim]{tokenizer}  
...  
\newcounter{rrCount}  
\newcommand{\replace}[1]{%  
    \GetTokens{rrFirst}{rrRest}{#1,}%  
    \textbf{\rrFirst}%  
    \forloop{rrCount}{0}{\value{rrCount} < 100}{%  
        \ifthenelse{\equal{\rrRest}{}}{%  
            \setcounter{rrCount}{101}%  
        }{%  
            \GetTokens{rrFirst}{rrRest}{\rrRest}%  
            $\rightarrow$\textbf{\rrFirst}%  
        }%  
    }%  
}%  
% -----------------------------------------------------------------  
\replace{a1}\\  
\replace{a2,b2}\\  
\replace{a3,b3,c3}\\  
Paul Biggar

Try the xstring package:

\usepackage{xstring}

[…]

\StrSubstitute{File,New}{,}{\(\rightarrow\)}

OK, I withdraw this answer. Thanks for clarifying the question.


I suspect this may not be what you want, but here goes anyway:

\newcommand{\replace}[2]{\textbf{#1}$\rightarrow$\textbf{#2}}  
\replace{File}{New}  

If this isn't what you're looking for, could you clarify the question, please?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!