Skip to content

Commit

Permalink
Remove LaTeX #518
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-kolb committed May 25, 2016
1 parent 4a6f191 commit 03bbc8f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package net.sf.jabref.logic.formatter.bibtexfields;

import net.sf.jabref.logic.formatter.Formatter;
import net.sf.jabref.logic.l10n.Localization;

public class RemoveLatexFormatter implements Formatter {

@Override
public String getName() {
return Localization.lang("LaTeX remover");
}

@Override
public String getKey() {
return "remove_latex";
}

@Override
public String format(String oldString) {
String newValue = oldString;

// Remove redundant $, {, and }, but not if the } is part of a command argument: \mbox{-}{GPS} should not be adjusted
newValue = newValue.replace("$$", "").replaceAll("(?<!\\\\[\\p{Alpha}]{0,100}\\{[^\\}]{0,100})\\}([-/ ]?)\\{",
"$1");
// Move numbers, +, -, /, and brackets into equations
newValue = newValue.replaceAll("(([^$]|\\\\\\$)*)\\$", "$1@@"); // Replace $, but not \$ with @@
newValue = newValue.replaceAll("([^@]*)@@([^@]*)@@", "$1\\$$2@@"); // Replace every other @@ with $
//newValue = newValue.replaceAll("([0-9\\(\\.]+) \\$","\\$$1\\\\ "); // Move numbers followed by a space left of $ inside the equation, e.g., 0.35 $\mu$m
newValue = newValue.replaceAll("([0-9\\(\\.]+[ ]?[-+/]?[ ]?)\\$", "\\$$1"); // Move numbers, possibly with operators +, -, or /, left of $ into the equation
newValue = newValue.replaceAll("@@([ ]?[-+/]?[ ]?[0-9\\)\\.]+)", " $1@@"); // Move numbers right of @@ into the equation
newValue = newValue.replace("@@", "$"); // Replace all @@ with $
newValue = newValue.replace(" ", " "); // Clean up
newValue = newValue.replace("$$", "");
newValue = newValue.replace(" )$", ")$");
return newValue;
}

@Override
public String getDescription() {
return Localization.lang("Removes LaTeX code.");
}

@Override
public String getExampleInput() {
return "\textit{Nucleus} {API}";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net.sf.jabref.logic.formatter.bibtexfields;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* Tests in addition to the general tests from {@link net.sf.jabref.logic.formatter.FormatterTest}
*/
public class RemoveLatexFormatterTest {

private final RemoveLatexFormatter formatter = new RemoveLatexFormatter();

@Test
public void test() {
assertEquals("$\\alpha\\beta$", formatter.format("$\\alpha$$\\beta$"));
}

@Test
public void formatExample() {
assertEquals("Nucleus API", formatter.format(formatter.getExampleInput()));
}


}

1 comment on commit 03bbc8f

@koppor
Copy link
Member

@koppor koppor commented on 03bbc8f Jun 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code looks similar to net.sf.jabref.logic.formatter.bibtexfields.LatexCleanupFormatter. I think, it is just a code start.

I wonder whether your desired functionality is achieved by net.sf.jabref.logic.layout.format.LatexToUnicodeFormatter (old name: FormatChars)?

Or is this code here obsolete? If not, could you add some comments on the relation to the other formatters? 😇

Please sign in to comment.