/** @param sep the pane where to remove the LaTeXViewer */
 public static void removeLaTeXViewer(JTextComponent tc) {
   if (viewer.icon != null) {
     viewer.icon = null;
     viewer.setVisible(false);
     tc.remove(viewer);
     tc.repaint();
   }
 }
  /**
   * @param sep the pane where to display the LaTeX
   * @param exp the expression
   * @param b the beginning of the expression
   * @param e the end of the expression
   */
  public static int displayExpression(
      JTextComponent tc, int totalHeight, String exp, int b, int e) {
    String latex = exp;
    if (exp.startsWith("$") || exp.startsWith("\"$") || exp.startsWith("'$")) {
      int n = 1;
      // We have $...$ or "$...$"
      if (exp.charAt(0) != '$') {
        n = 2;
      }
      latex = exp.substring(n, exp.length() - n);
    }

    viewer.icon = ScilabSpecialTextUtilities.compilePartialLaTeXExpression(latex, defaultSize);
    if (viewer.icon == null) {
      return 0;
    }

    width = viewer.icon.getIconWidth();
    height = viewer.icon.getIconHeight();
    viewer.setSize(width + 2 * INSET, height + 2 * INSET);
    tc.add(viewer);
    Rectangle begin;
    Rectangle end;
    try {
      begin = tc.modelToView(b);
      end = tc.modelToView(e);
    } catch (BadLocationException ex) {
      begin = NULLRECT;
      end = NULLRECT;
    }

    int abs;
    int ord;
    if (begin.y == end.y) {
      // We're on the same line
      abs = Math.max(1, (end.x + begin.x - width) / 2);
      ord = begin.y + begin.height + 1;
      if (height + ord > totalHeight) {
        ord = begin.y - 1 - height - 2 * INSET;
      }
    } else if (begin.y + begin.height == end.y) {
      // The line is drawn on two lines
      ord = end.y + end.height + 1;
      if (height + ord > totalHeight) {
        ord = begin.y - 1 - height - 2 * INSET;
        abs = begin.x;
      } else {
        abs = end.x;
      }
    } else {
      // Guess
      abs = Math.max(1, (tc.getWidth() - width) / 2);
      ord = end.y + end.height + 1;
      if (height + ord > totalHeight) {
        ord = begin.y - 1 - height - 2 * INSET;
      }
    }

    viewer.setLocation(abs, ord);
    viewer.setVisible(true);
    viewer.repaint();

    return height + 2 * INSET;
  }
 /**
  * @param sep the pane where to display the LaTeX
  * @param exp the expression
  * @param b the beginning of the expression
  * @param e the end of the expression
  */
 public static void displayExpressionIfVisible(
     JTextComponent tc, int totalHeight, String exp, int b, int e) {
   if (viewer.isVisible()) {
     displayExpression(tc, totalHeight, exp, b, e);
   }
 }
 /**
  * Set the default size to render LaTeX
  *
  * @param size the font size
  */
 public static void setDefaultSize(int size) {
   viewer.defaultSize = size;
 }