1

So I need to write a programme on c++ that converts given latex file with tags etc., but it only contains text (no mathematical equations). It should create a txt file without any tags, only plain text. I see I should use the parser here, but I don't know how to write it and what to do next.How exactly do I write a parser for converting latex into plain text? Example of the tex file:

%% -*- coding: cp866 -*- 
\documentclass[12pt,a4paper]{article} 
\usepackage[cp866]{inputenc} 
\usepackage[russian]{babel} 
\usepackage{graphicx} 
\usepackage{misccorr} 
\usepackage{amsmath} 
\usepackage{contour} 
\begin{document} 
\begin{center} 
\large 
RUSSIAN FEDERATION 
\end{center} 
\large 
\textbf{Surname:}\hspace{0.25cm}\\ 
\LARGE 
tit\\ 
\large 
\textbf{Given names:}\hspace{0.25cm}\\ 
\LARGE 
fgfg\\ 
\large 
\textbf{Nationality:}\hspace{0.25cm}\\ 
\LARGE 
gdh\\ 
\large 
\textbf{Date of birth:}\hspace{0.25cm}\\ 
\LARGE 
fghfh\\ 
\large 
\textbf{Sex:}\hspace{0.25cm}\\ 
\LARGE 
ghfghb\\ 
\large 
\textbf{Place of birth:}\hspace{0.25cm}\\ 
\LARGE 
hfgh 
\end{document}
  • 2
    Welcome to TeX.SE. It would be helpful if you composed a fully compilable MWE including \documentclass and the appropriate packages that shows the type of file you want to convert to plain text. I have a feeling that a general C++ parser is going to be quite difficult. Probably much easier to get TeX to generate the plain text file, but without an example of such a document it is difficult to know where to start on this issue. – Peter Grill Oct 24 '18 at 18:55
  • 1
    If you want an example of how difficult an external parser can be, have a look at xii.tex. Actually that version appears too be in English, so see the link in that answer. – Peter Grill Oct 24 '18 at 18:57
  • 1
    Why not compile the code to produce a PDF then convert it to text? – Werner Oct 24 '18 at 18:58
  • 2
    is there any reason not to use an existing program, why write a new C++ program? you could use latex followed by dvi2tty or you could use pdflatex followed by pdftotext or several other methods – David Carlisle Oct 24 '18 at 20:06
  • 1
    do your real examples have \\ and \large every other line? the markup is very strange. – David Carlisle Oct 24 '18 at 20:07
3
 latex file; dvi2tty file 

produces the first block below,

pdflatex file; pdftotext file.pdf

produces the second block.

these are commandline commands but of course you could call them from a C++ program if neeed.

                      RUSSIAN FEDERATION



Surname:


tit


Given names:


fgfg


Nationality:


gdh


Date of birth:


fghfh


Sex:


ghfghb


Place of birth:


hfgh



                                     1

RUSSIAN FEDERATION
Surname:

tit
Given names:

fgfg
Nationality:

gdh
Date of birth:

fghfh
Sex:

ghfghb
Place of birth:

hfgh

1

  • That is the problem, I can't use any other programme, only write a new one. I am new in this language, so sorry for the strange code (would be great if you correct it where needed). I think the best way for me to understand is to write a parser by analysing the code by symbol (the longest, too, but I am a newbie, so...) – Liz Oct 24 '18 at 20:19
  • 1
    @Liz writing a parser for tex from scratch is a multi-man year project. Are you sure you want to do that? – David Carlisle Oct 24 '18 at 20:21
  • ugh, guess not? Can't I generate it using yacc/lex and bison? Or maybe there is other way to write it without other programs? – Liz Oct 24 '18 at 20:23
  • @Liz no the tex language is far too context sensitive to use a yacc based parser. – David Carlisle Oct 24 '18 at 20:24
  • 2
    @Liz my advice is to change the problem, You haven't given any reason why you need no external tools. Perhaps your tex is really simple and you can write a simple c program to get what you want. For example in your example you could just write something that removed all tex commands and {} and left the remaining text, and it would be more or less what you want as the example doesn't really use any tex other than font commands and spacing that you want to ignore. But if you want to accept general latex documents you need to use tex or write a new tex implementation there is no other way. – David Carlisle Oct 24 '18 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.