2

Thanks to this great site I've learned that in LaTeX one should always use \[...\] pair for equations typesetting instead of $$...$$. Unfortunately it is too late: I already have a big text with this obsolete markup, split in multiple source files.

So what is the most elegant way of fixing this issue? I definitely need some preprocessor, and think that awk might be the tool of choice. What do you suggest?

faleichik
  • 231
  • My first thought was sed and some regexp – Argo Aug 14 '14 at 15:04
  • 2
    I guess the answer will depend importantly on how you've been inputing the $$ doublets so far: Are they always/usually/rarely/never at the start and end of a line, or can they be anywhere? The less structure you've been using so far in handling your TeX input, the more elaborate the replacement rule will have to be. Please advise on this aspect of your typing habits. – Mico Aug 14 '14 at 15:08
  • Apart from the precise replacement, I would suggest a wrapper command \prettymath{...} , which expands to \[...\], because this easier to detect the next time you want to change such mathematical markup –  Aug 14 '14 at 15:08
  • @Mico: I'd like to have a universal solution, independent of the $$ placement. – faleichik Aug 14 '14 at 15:10
  • 2
    @faleichik: The problem is, that a universal solution is perhaps very complicated. –  Aug 14 '14 at 15:12
  • http://tex.stackexchange.com/questions/194001/ – Manuel Aug 14 '14 at 15:12
  • @Manuel: Nice question and nice solutions there, but I think, the $$...$$ style should not only gobbled away, but disappear completely, as if never been written at all –  Aug 14 '14 at 15:20
  • I'd just write a simple program, e.g. in C, to replace all odd occurrences with \[ and all even ones with \]. – JPi Aug 14 '14 at 15:22
  • @ChristianHupfer I don't see a reason for that. It's just a different “user interface”. It's not recommended because in the actual LaTeX2e it leads to the untouched TeX $$ … $$ which seems to behave worse than \[ … \]. But if it behaved correctly… what's wrong with that input? – Manuel Aug 14 '14 at 15:22
  • 1
    Take a look at https://dl.dropboxusercontent.com/u/3825336/TeX/Files/dedollar.zip Disclaimer: not tested. – giordano Aug 14 '14 at 15:23
  • 2
    @JPi: How should the C program know, in which context $$ occurs? Is it in a verbatim env? Is it the starter of $$...$$ or the end code of it? It's not that easy –  Aug 14 '14 at 15:28
  • Consider doing the replacements one file at a time - when you open the file to make other changes. Your full document will still compile all the time, and you'll spread out the nuisance overhead of the update. You could use search/replace in your editor, or an easy awk program - be sure to back up the original first. – Ethan Bolker Aug 14 '14 at 15:54
  • @EthanBolker Agree 100% about backup. But spreading the nuisance out seems pointless if you use something like awk which can be run from the command line on multiple files in one go - no need to open each one and apply the script separately. But this makes it essential that backups are created since you will lose the ability to revert if you simply replace files in place. – cfr Aug 14 '14 at 16:01
  • @JPi This also assumes that $$ is never part of a comment or, if it is, that it is always matched if so. As others have said, a general solution would require a sophisticated approach with a degree of complexity which is almost certainly unnecessary if the OP, like most people, has at least some formatting habits so at least some regularities in their source code. – cfr Aug 14 '14 at 16:05
  • @cfr: right, a completely general solution is hard, but unnecessary. – JPi Aug 14 '14 at 16:26
  • I don't think this is a duplicate. Doesn't that question ask how to effectively replace them during compilation? As I understand it, this question is about substituting the characters in the actual source and not just having them translated at compilation time. (As such, the question is, however, arguably off-topic for this site since the best solutions are unlikely to use TeX.) – cfr Aug 14 '14 at 17:16
  • @cfr I think the “substituting characters in the actual source” is the solution OP came up to, not necessarily what he is asking about. By the way, in my opinion, that question is the one that asks to preprocess the file, rather than this question (which doesn't ask for anything explicit but what is the most elegant way of fixing this issue?). TL;DR It might be true that it's not a duplicate, but just for the opposite reason you said. I understood this in a second read just now. – Manuel Aug 14 '14 at 17:21
  • @Manuel I may be confusing it with the other question linked from your comment above. I took 'fixing the issue' to refer to the state of the source. But I think you could read it the other way, so now I think the question is ambiguous. (It could be specific one way or the other, or it could be general.) – cfr Aug 14 '14 at 17:24
  • @cfr No, I was talking about the one that this is a duplicate of. The other one is the one that wants to change the source code, this questions asks just what is a good way to solve the issue, offering the option OP came up with: using some script to change the source, but not limiting. – Manuel Aug 14 '14 at 17:27

1 Answers1

0

May be redefining $ so it behaves like \[ and \]?

This is an option (although it probably isn't perfect and give problems, it could possibly be enhaced easily).

\usepackage{fixltx2e}
\usepackage{mathtools}

\makeatletter
\catcode`\$=\active
\protected\def${\new@ifnextchar$\faleichik@dmath\faleichik@math}
\def\faleichik@math#1${\(#1\)}
\def\faleichik@dmath$#1$${\[#1\]}
\makeatother
Manuel
  • 27,118
  • 2
    Could be dangerous---there's no way to be certain what other packages are using. – Sean Allred Aug 14 '14 at 15:50
  • Almost everything borderline could be dangerous in that way. In my case, it compiled fine two “student books” of 150 pages each, full of inline and display math. But hey, there's nothing stopping the OP from trying and seeing if it gives results. – Manuel Aug 14 '14 at 16:01
  • Very true. It's a useful tactic to keep in mind :) – Sean Allred Aug 14 '14 at 16:50