# Output Latex into single words and graphs

Is it possible to change the output of a LaTex file so that it generates one file per word and per graph?

Instead of generating a single file that looks like this:

 _______________________________________
|                                       |
| 1. Word1 word2 word3 word4            |
|    a. Word5 word6 word7               |
|                                       |
|   ///////////Graph1///////////        |
|                                       |
|    b. Word8 word9 word10              |
|                                       |
| 2. Word11 word12 word13 word14        |
|                                       |
|_______________________________________|


I would like to generate multiple files that look like this:

 _______________________________________
|          |       |       |            |
| 1. Word1 | word2 | word3 | word4      |
|__________|_______|_______|____________|
|             |       |                 |
|    a. Word5 | word6 | word7           |
|_____________|_______|_________________|
|                                       |
|   ///////////Graph1///////////        |
|_______________________________________|
|             |       |                 |
|    b. Word8 | word9 | word10          |
|_____________|_______|_________________|
|           |        |        |         |
| 2. Word11 | word12 | word13 | word14  |
|___________|________|________|_________|


Do you think this is a possible type of output for LaTex files? How would you do this?

Here the kind of output I am working on:

• If you don't get answers soon, maybe make your question much simpler. Right now I think that there is way too much information that is not relevant. – Dr. Manuel Kuehner Aug 19 '17 at 14:21
• Ok thank you, I might break this question into smaller bits, just like I need to break a large LaTex into smaller outputs :) – enzolito Aug 19 '17 at 15:06
• Should I completely re-edit this question or ask a new one? – enzolito Aug 20 '17 at 8:58
• I would vote for re-edit – Dr. Manuel Kuehner Aug 20 '17 at 9:01
• I suspect you are really interested in the pictures. A much more reasonable way to obtain them all, with one page per picture would be to process (lpeg is nice for this) the source tex file to output only the (presumably pspictures), and then compile the output in a standalone class. – marsupilam Aug 20 '17 at 12:30

## 2 Answers

Here is a way of doing it using the optional package and a helper bash script.

Your document would look like this:

\documentclass{article}

\usepackage{optional}

\begin{document}

Content in the document scope will be included in every version.

\opt{opta}%
{
This is some content selected by opta.
}

\opt{optb}%
{%
This is some content selected by optb.
}

Some more content in the global scope.

\opt{opta}%
{
More content selected by opta.
}

\opt{optb}%
{
More content selected by optb.
}

More content in the global scope.

\end{document}


We assume you save it as myfile.tex.

Now, save the following as helper.sh:

#!/bin/bash
inputfile=${1%.tex} # find all \opt declarations, remove duplicates optional=$(grep -P '\\opt\{\w+\}+' $inputfile.tex | awk -F '[{}]' '{print$(NF-1)}'  | xargs -n1 | sort -u | xargs )

# compile and rename output separately for each optional declaration
for opt in $optional; do echo "processing part$opt"
pdflatex -interaction=nonstopmode "\def\UseOption{$opt}\input{$inputfile}" > /dev/null
mv $inputfile.pdf$opt.pdf
done


Make the script executable with

chmod +x helper.sh


and invoke it with

./helper.sh myfile.tex


You should then end up with separate pdf files opta.pdf and optb.pdf with the corresponding content.

Note that, because of the way I wrote the bash script, you must use the form above

\opt{opta}%
{
This is the content selected by opta.
}


rather than

\opt{opta}{
This is the content selected by opta.
}


If you do put the second opening brace on the same line, then the pattern matching will not work. Maybe someone with more script-fu than me can fix this.

• Thank you Michael. I should probably have been clearer but the objective is to get every single word or graph in the Tex document as a separate file. The reason is I would like to display them as images and re-arrange them dynamically in a web app. I'm not sure your approach gets me there. – enzolito Aug 20 '17 at 17:37
• @enzolito - no, in that case, it won't work this way - at least not for the words, although it might still be useful for the graphs. For the words, you will have to think of something else. – Michael Palmer Aug 20 '17 at 20:00

This is in response to your comment on my first answer.

Here is a horrible hack that you might find useful - or not. It only deals with extracting single words; if you find it essential to extract graphics automatically from the same document, you must extend this in some way.

We use a boolean switch to either produce a regular document (\setboolean{mangle}{false}) or to mangle it such that each word appears on a separate tiny page (\setboolean{mangle}{true}).

We also abuse \makeindex to have an index entry written to file for every word.

\documentclass{article}
\usepackage{geometry, ifthen, xparse}
\makeindex

\newboolean{mangle}
\setboolean{mangle}{true}% false for regular output

\ifthenelse{\boolean{mangle}}{\geometry{papersize={3in,1in},margin=0.2in}}

\newcommand{\doword}[1]{%
\ifthenelse{\boolean{mangle}}%
{\clearpage\fbox{#1\strut}\index{#1}\clearpage}%
{ #1}%
}

\ExplSyntaxOn
\NewDocumentCommand {\dowords}
{ > { \SplitList { ~ } } m }
{ \tl_map_inline:nn {#1} { \doword{##1} }}
\ExplSyntaxOff

\begin{document}

\dowords{A stretch of words typed into the document}.

\dowords{And some more stuff}.

\dowords{Note that we keep the punctuation separate},\dowords{because otherwise we will end up in trouble}.

\dowords{We can also put some math here $\frac{1}{2}$ and see what happens}.

\end{document}


If you run this in mangle mode, you will end up with a PDF that has one word per page, inside an fbox and with a strut, so that the baseline and image height will be consistent between 'sauce', 'egg' and 'leg'. You will also have an .idx file that looks as follows:

\indexentry{put}{35}
\indexentry{some}{36}
\indexentry{math}{37}
\indexentry{here}{38}
\indexentry{${\begingroup 1\endgroup \over 2}$}{39}
\indexentry{and}{40}


The idea for post-processing is as follows:

• batch-convert the pdf file to bitmap. Imagemagick can produce numbered .png files from the pages of a multi-page pdf file. It can also trim off the whitespace and the fbox around each word.

• use a script (Python/Bash/whatever) to extract the words that were typeset and their corresponding page numbers from the .idx file. Retrieve the .png file for that page number and save under the appropriate word name. Obviously, you would have to get a bit creative with file naming where the input is something like ${\begingroup 1\endgroup \over 2}$.

• you probably also want to deal with redundant words somehow. The best way might be to use Luatex and store each new word and only output it once.

This is the best I can think of, but overall I'd say it's still pretty bad :-/ I would try to think of some more straightforward approach w/o using LaTeX to move my single words around on a web page.