public void ParseHeader() { #region Regexs // \thing{blah} string regexPattern = @"\\(w+)\{(\w+)\}"; //\thing[optionalBlah]{blah} string regexPatternOptionals = @"\\(\w+)\[(\w+)\]\{(\w+)\}"; //\thing{blah1}{blah2} string regexPatternMacro = @"\\(\w+)\{(\w+)\}\{(\w+)\}"; //\thing string regexPatternNoParams = @"\\(\w+)"; #endregion Regexs bool done = false; // keep reading header information until I say so! // Let us begin... using (StreamReader r = this) { StringBuilder sb = new StringBuilder(); while (!done && !r.EndOfStream) { string line = r.ReadLine(); // We should ignore the line if it's empty and move on. while (line == Environment.NewLine) { line = r.ReadLine(); } // We need to make sure we have the whole statement. while (line.Replace("\n", "").Replace("\r", "").Replace(" ", "").Length > 0 || (TeX.Count(line, new List <char>() { '{', '[' }) != TeX.Count(line, new List <char>() { '}', ']' }))) { line += r.ReadLine(); } // Let's remove any newLine characters. line.Replace("\n", "").Replace("\r", ""); while (!r.EndOfStream && line[0] == '%') { line = ReadLine(); } if (line == @"\\begin{document}") { // We're finished, move onto the next line and let's get outta here. done = true; r.ReadLine(); break; } else if (Regex.IsMatch(line, regexPatternMacro)) { } else if (Regex.IsMatch(line, regexPatternOptionals)) { } else if (Regex.IsMatch(line, regexPattern)) { } else if (Regex.IsMatch(line, regexPatternNoParams)) { } else { throw new Exception(string.Format("I have no idea what \"{0}\" is in terms of header information.", line)); } if (r.EndOfStream) { throw new Exception("Got through the whole file without a \\begin{Document}, This means something went wrong or your input file wasn't right!"); } } } }