3

Lets assume we want to write an article. A third party asks us to submit the abstract as a text file.

Can I automatically export the abstract of a LaTeX document to a text file? Ideally, the abbreviations or other LaTeX macros are expanded before export.

Example code:

\documentclass{article}
\usepackage{siunitx}
\usepackage[acronym, toc, nonumberlist]{glossaries}
\title{Title of Document}
\author{Name of Author}

\newcommand{\texttest}{TEST}
\newacronym{BLSTM}{BLSTM}{bidirectional long short term memory network}

\begin{document}
\maketitle

\begin{abstract}
This is an abstract \texttest~with interesting abbreviations like \gls{BLSTM} and other macros like $\SI{15}{\second}$.
\end{abstract}

\end{document}

Expected output:

This is an abstract TEST with interesting abbreviations like bidirectional long short term memory network (BLSTM) and other macros like 15 s.
  • I think it would be better to look for a pdf or dvi converter. – touhami Mar 14 '18 at 20:08
6
+100

You must to maintain the \abstractdefs macro where special definitions are in order to expand to the desired output. The example below shows what you need to insert to the \abstractdefs in order to realize your example above. Moreover, there is a \stipdollars macro which removes dollars from the output.

\documentclass{article}
\usepackage{siunitx}
\usepackage{environ}
\usepackage[acronym, toc, nonumberlist]{glossaries}
\title{Title of Document}
\author{Name of Author}

\newwrite\absfile
\NewEnviron{Abstract}{
   \immediate\openout\absfile=\jobname.abs
   \begingroup
      \def\tmp{}\abstractdefs 
      \expandafter\stripdollars\BODY$\relax
      \immediate\write\absfile{\tmp}%
   \endgroup
   \abstract\BODY\endabstract
}
\long\def\stripdollars#1${\edef\tmp{\tmp#1}\stripdollarsA}
\def\stripdollarsA{\futurelet\next\stripdollarsB}
\def\stripdollarsB{\ifx\next\relax\else \expandafter\stripdollars\fi}

\def\abstractdefs{
   \def~{ }
   \def\gls##1{##1}
   \def\SI##1##2{##1 ##2}
   \def\second{s}
   \def\par{^^J}
   \def\TeX{TeX}
}

\newcommand{\texttest}{TEST}
\newacronym{BLSTM}{BLSTM}{bidirectional long short term memory network}

\begin{document}
\maketitle

\begin{Abstract}
This is an abstract \texttest~with interesting abbreviations like \gls{BLSTM} 
\end{Abstract}

\end{document}
  • This works very well. Can you elaborate why \def\par{^^J} avoids \par to appearch in the output file? – Lukas Mar 15 '18 at 9:05
  • 1
    ^J (input as ^^J here) is the ASCII line feed control character, so \par is effectively replaced by a newline character. See: en.wikipedia.org/wiki/Control_character – Circumscribe Mar 15 '18 at 9:50
4

I don't know of a package that does this and having all commands in the abstract expand to plain text is probably not possible because latex is not designed to turn commands into plain text.

Here is a solution that writes your abstract to a file, expanding "what it can". It uses the environ package to define a new Abstract environment and the newfile package to write the abstract to a file and "expand" the abstract in some sense. Using the the Abstract envinment the abstract appears as it normally does in the PDF file and a partially expanded version of the abstract is written to \jobname.abs (that is, the name of the LaTeX file with an abs extension. For the abstract in the original file, this produces the following abs file:

This is an abstract TEST\nobreakspace {}with interesting abbreviations like \gls {BLSTM} and other macros like $\SI {15}{\second }$.

So, some expansion has occurred but there is a cost with primitives like \nobreakspace appearing in the output together with \gls and \SI commands. I do not know how to do better than this.

Here is the full code:

\documentclass{article}
\usepackage{siunitx}
\usepackage{environ}
\usepackage{newfile}
\usepackage[acronym, toc, nonumberlist]{glossaries}
\title{Title of Document}
\author{Name of Author}

\newoutputstream{abstract}% define a new output file stream
\NewEnviron{Abstract}{
  \openoutputfile{\jobname.abs}{abstract}% open the abs file
  \addtostream{abstract}{\BODY}% write the abstract=\BODY to the file
  \closeoutputstream{abstract}%  close the file
  \abstract\BODY\endabstract%    add the abstract to the PDF file
}

\newcommand{\texttest}{TEST}
\newacronym{BLSTM}{BLSTM}{bidirectional long short term memory network}

\begin{document}
\maketitle

\begin{Abstract}
This is an abstract \texttest~with interesting abbreviations like \gls{BLSTM} and other macros like $\SI{15}{\second}$.
\end{Abstract}

\end{document}
1

This can likely be accomplished using Pandoc, as described in a couple of related questions.

You should be able to do something like:

pandoc -s example.tex -o /dev/stdout | grep -P abstract -A 2

I have not been able to try this yet, but will update this answer if I get the opportunity to do so.

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.