Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating a LaTeX file, java

So I have this text file

\begin{document}
    {\Large \begin{center} Homework Problems \end{center}}\begin{itemize}\item\end{itemize}
    \begin{enumerate}
                    \begin{proof}
                            \begin{align}

                            \end{align}
                    \end{proof}

                    \begin{proof}

                            \begin{align}

                            \end{align}

                    \end{proof}
    \end{enumerate}
\end{document}

And I want to go through each line, find all of the "\begin" pieces and then take the string with in the "{_}" and store it in a stack. When the corresponding "\end" is found, I call the pop() command on the Stack and remove it. I'm have a few issues though...

  1. I'm running into dealing with all sorts of crazy cases and making sure everything is accommodated and its becoming too specific to this case when I want to make it work for all sorts of files that are written as such.
  2. I don't know how to check for "\begin" and "\end" as opposed to "begin" and "end", the reason this is important is because if the file contains text that says "begins" or "end" it might not be a command and thus, not what I'm looking for.

All "if" statements DO NOT work on account of the "\" being present, I tried adding square brackets but it didn't fix anything.

Here is my code so far, and its getting really confusing, can anyone help organize and help rectify the issues I've stated above?

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;

public class LaTeXParser{

public static void main(String args[]) throws FileNotFoundException{

    Scanner scan = new Scanner(System.in);

    Stack s = new Stack();

    int lineCount = 0;

    String line;
    String nextData = null;
    String title = null;

            String fname;

            System.out.print("Enter the name of the file (no extension): ");
            fname = scan.next();

            fname = fname + ".txt";

            FileInputStream fstream = new FileInputStream(fname);

            Scanner fscan = new Scanner(fstream);

            System.out.println();

            while(fscan.hasNextLine()){

                lineCount++;
                line = fscan.nextLine();
                StringTokenizer tok = new StringTokenizer(line);

                while(tok.hasMoreElements()){

                    nextData = tok.nextToken();
                    System.out.println("The line: "+nextData);

                    if(nextData.contains("\\begin") && !nextData.contains("\\end")){

                        if(nextData.charAt(1) == 'b'){

                            title = nextData.substring(nextData.indexOf("{") + 1, nextData.indexOf("}"));

                            s.push(title);

                        }

                        else{

                            //title = nextData.substring();

                        }
                    }//end of BEGIN if

                    if(nextData.contains("\\end") && !nextData.contains("\\begin")){

                        if(s.peek().equals(nextData.substring(nextData.indexOf("{") + 1, nextData.indexOf("}")))){

                            s.pop();

                        }
                    }//end of END if

                    if(nextData.contains("\\begin") && nextData.contains("\\end")){

                        String[] theLine = nextData.split("[{}]");

                        for(int i = 0 ; i < theLine.length ; i++){

                            if(theLine[i].equals("\\end") && theLine[i+1].equals(s.peek())){

                                s.pop();

                            }

                            if(theLine[i].equals("\\begin")){

                                title = theLine[i+1];

                                s.push(title);

                            }


                        }

                    }//end of BEGIN AND END if

                }
            }//end of whiles

            fscan.close();

    while(!s.isEmpty()){

        System.out.println("the top "+s.pop());

    }
}
}

EDIT: In the if statement that is used to check a line to see if it contains both a "\begin" and "\end" after finding the "\begin", how do I go back through to check if that line also contains it's "\end"? So I am talking about the case...

\begin{itemize}\item\end{itemize}

See I can get to the "\begin" and add the proper string, but it just moves and passes the "\end{itemize}". Anyway to fix this?

Actually it should check and perform normally even after the "itemize" string is pushed, but it doesn't work! I believe it has to do with "\end", can anyone confirm? It skips over that step, and obviously because it doesn't fit the conditions, but it works for the other lines. Just not this specific case!

like image 886
Sherifftwinkie Avatar asked Aug 06 '26 05:08

Sherifftwinkie


1 Answers

You probably need to escape the backslashes, so write \\ instead of \. And if they are in regular expressions (regexprs) you need to escape them twice: \\\\ ; I don't think the brackets are needed.

like image 92
Basile Starynkevitch Avatar answered Aug 07 '26 18:08

Basile Starynkevitch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!