Text FormattingAutomatically add white space

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
SkyHiRider
Posts: 11
Joined: Mon Oct 18, 2010 2:14 pm

Automatically add white space

Post by SkyHiRider »

I need to add ~ between a single letter word and another word so they don't get separated. When the letter and word is between \begin{verbatim} and \end{verbatim}, it should be ignored as that is source code that doesn't need white spaces.

Is there a good application or command line tool that can easily achieve this ?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
west.logan
Posts: 87
Joined: Tue Sep 14, 2010 6:58 pm

Re: Automatically add white space

Post by west.logan »

I'm not certain what is being asked here. Could you give an example?

If I understand, you want to automatically insert a ~ between two words, say "Donald" and "Knuth", so that every instance they appear, they will always be on the same line together.

If that is the case, could you not simply define your own command so that \dk inserts "Donald~Knuth" or do a search and replace? Again, perhaps I'm not understanding the problem.
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Automatically add white space

Post by frabjous »

I think the issue is that the source document is already written and SkyHiRider now wants to change something globally using a find and replace, e.g., change "Donald Knuth" everywhere to "Donald~Knuth" except where "Donald Knuth" appears inside a verbatim environment, which case it should be left as Donald Knuth.

Sure, this can be done in a variety of ways. For example, with sed:

Code: Select all

sed '/\\begin{verbatim}/,/\\end{verbatim}/ !s/Donald Knuth/Donald~Knuth/g' input.tex > output.tex
That will take input.tex as input and substitute "Donald Knuth" with "Donald~Knuth" everywhere except between \begin{verbatim} and \end{verbatim}, and output a file named output.tex. (If you just want to alter input.tex without outputting a separate file -- be careful you know what you're doing! -- then put "-i" right after "sed" and leave out the "> output.tex" part.

But if you're not using a unix-based operating system, and don't have access to sed, I think I know how to do something similar with vim. If neither of those are options, certainly it could be done with something like perl. But why don't you tell us what operating system you are using?
Post Reply