1

I want LaTeX to automatically replace strings like " a ", " s ", " z " with " a~", " s~", " z~", because they can't be at line end. Any suggestions?

4
  • 4
    Wouldn't it be easier to use your editors regex replace function? Commented Nov 28, 2009 at 16:21
  • 2
    perl -p -i -e 's/a/a~/g' ?? Commented Nov 30, 2009 at 21:36
  • @Mica: I think he's looking more for /^Wa / to match on. Commented Dec 9, 2009 at 23:05
  • I’m voting to close this question because tex.stackexchange.com exists. Commented Aug 25, 2022 at 6:14

7 Answers 7

3

For Czech typographic rules, there is a preprocessor called Vlna" by Petr Olšák - download . The set of (usually prepositions in czech) is customizable - so it might be usable for other languages as well.

Sign up to request clarification or add additional context in comments.

1 Comment

It seems you interpreted why the OP wanted this behavior and approached the problem with that in mind. Seems like a very useful link.
2

You can use \StrSubstitute from xstring package.

e.g.

\StrSubstitute{change ME}{ ME}{d}

will convert change ME into changed. Although, nesting is not possible, so to make another substitution you must use an intermediate variable in this way

\StrSubstitute{change ME}{ ME}{d}[\mystring]
\StrSubstitute{\mystring}{ed}{ing}

Finally, your solution would be

\usepackage{xstring}
\def\mystring{...source string here...}
\begin{document}
\StrSubstitute{\mystring}{ a }{a~{}}[\mystring]
\StrSubstitute{\mystring}{ s }{s~{}}[\mystring]
\StrSubstitute{\mystring}{ z }{z~{}}[\mystring]
\mystring
\end{document}
  • Note the use of the empty string {} to avoid the sequence ~}.

Comments

1

I'm afraid (to the best of my knowledge) this is basically impossible with LaTeX. A LuaTeX-based solution might be possible, though.

It's not actually clear to me, however, that " a ", for example, shouldn't appear at the end of a
line. Although I might be used to different typographic rules.

(Is there anything wrong with the line break in the last paragraph? :))

Comments

0

As far as I know there is no way to do this in LaTeX itself. I'd go for automating this with some external tools, as my typical setup involves a Makefile handling the LaTeX run by itself. This makes it rather easy to run tools like sed on the sources and do some replacements using regular expressions, and a simple rule would do this for your case.

If you use some LaTeX editor that does everything for you you should check the editors regular expression search and replace functionality.

Comments

0

Yes, this is the age old argument of data processing vs. data composition. We have always done these things in a pre-processor environment responsible for extracting the information from its source environment, SQL or plain-text, and created the contents of a \input(file.tex).

But yes, it is possible (TeX is after all a programming language) but you will have to become a wizard. Get the 4 volume set TeX in Practice by Stephan von Bechtolsheim.

The approach would be to begin an environment (execute a macro) whose ''argument'' was all text down to the end of the environment. Then just munge though the tokens fixing the ones you want.

Still, I don't think any of us are advocating this approach.

Comments

0

If you are using TeXmaker to write your LaTeX file, then you may click on the Edit button on the toolbar, then click on Replace.

A dialogue box will come up, and you can enter your strings one after the other.

You put the strings to be changed in the Find text input and what you want it to be changed to in the Replace text input.

You can also specify where you want the replacement to start from.

Comments

-3

Click Find and Replace (or similiar) in the menu of your text editor and do it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.