3

Under Windows 8, I am using R and the knitr package in RStudio with an .Rnw script. My regex skills are passable, but only in R.

My document has about thirty pages with a different text section at the bottom of each page. The text sections start the same, \subsubsection{\textcolor{blue}{\textsf{R} Note:, and end with \clearpage.

How can I extract all the text sections after "Note:" into a file as plain text?

Here is my MWE:

\documentclass[11pt]{article}     
\begin{document}

\section{How to Find and Extract Text within Fixed Markers}

\noindent\rule{150pt}{1.7pt}  
\vspace{2pt}
\subsubsection{\textcolor{blue}{\textsf{R} Note: History and Current Status of \textsf{R}}}:  \textsf{R} is an open source software platform used to analyze data.  Written originally in the early 1990's, it is an \textbf{object-oriented}, \textbf{interpreted} programming language blessed with a wealth of constantly-growing and improving "packages" and a vibrant user community.

\clearpage

\section{Each Note is at the bottom of a page.  All have the same start up to Note: They end with clearpage}

\noindent\rule{150pt}{1.7pt}  
\vspace{2pt}
\subsubsection{\textcolor{blue}{\textsf{R} Note: Different text}}:  Some other text.  **There are NO OTHER COMMANDS after the colon except \textsf{R} and all of it consists of ONE LINE OF TEXT.**

\clearpage

\end{document}

Several questions here seemed like they would guide me, on what may well be a very simple question, but I could not figure out what to do.

uses xstring package

extracting from a text string

relies on text being in an environment

HERE IS THE OUTPUT FILE:

This is XeTeX, Version 3.14159265-2.6-0.99991 (MiKTeX 2.9 64-bit) (preloaded format=xelatex 2015.12.3)  3 FEB 2016 20:03
entering extended mode
**soquestion.tex
(soquestion.tex
LaTeX2e <2014/05/01>
Babel <3.9l> and hyphenation patterns for 69 languages loaded.
("C:\Program Files\MiKTeX 2.9\tex\latex\base\article.cls"
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
("C:\Program Files\MiKTeX 2.9\tex\latex\base\size11.clo"
File: size11.clo 2014/09/29 v1.4h Standard LaTeX file (size option)
Requested font "cmr10" at 10.95pt
)
\c@part=\count80
\c@section=\count81
\c@subsection=\count82
\c@subsubsection=\count83
\c@paragraph=\count84
\c@subparagraph=\count85
\c@figure=\count86
\c@table=\count87
\abovecaptionskip=\skip41
\belowcaptionskip=\skip42
\bibindent=\dimen102
)
("C:\Program Files\MiKTeX 2.9\tex\latex\graphics\graphicx.sty"
Package: graphicx 2014/10/28 v1.0g Enhanced LaTeX Graphics (DPC,SPQR)

("C:\Program Files\MiKTeX 2.9\tex\latex\graphics\keyval.sty"
Package: keyval 2014/10/28 v1.15 key=value parser (DPC)
\KV@toks@=\toks14
)
("C:\Program Files\MiKTeX 2.9\tex\latex\graphics\graphics.sty"
Package: graphics 2014/10/28 v1.0p Standard LaTeX Graphics (DPC,SPQR)

("C:\Program Files\MiKTeX 2.9\tex\latex\graphics\trig.sty"
Package: trig 1999/03/16 v1.09 sin cos tan (DPC)
)
("C:\Program Files\MiKTeX 2.9\tex\latex\00miktex\graphics.cfg"
File: graphics.cfg 2007/01/18 v1.5 graphics configuration of teTeX/TeXLive
)
Package graphics Info: Driver file: xetex.def on input line 91.

("C:\Program Files\MiKTeX 2.9\tex\xelatex\xetex-def\xetex.def"
File: xetex.def 2015/03/25 v4.04 LaTeX color/graphics driver for XeTeX (TeX Liv
e/RRM/JK)
))
\Gin@req@height=\dimen103
\Gin@req@width=\dimen104
)
("C:\Program Files\MiKTeX 2.9\tex\latex\graphics\color.sty"
Package: color 2014/10/28 v1.1a Standard LaTeX Color (DPC)

("C:\Program Files\MiKTeX 2.9\tex\latex\00miktex\color.cfg"
File: color.cfg 2007/01/18 v1.5 color configuration of teTeX/TeXLive
)
Package color Info: Driver file: xetex.def on input line 137.
) (framed.sty
Package: framed 2011/10/22 v 0.96: framed or shaded text with page breaks
\OuterFrameSep=\skip43
\fb@frw=\dimen105
\fb@frh=\dimen106
\FrameRule=\dimen107
\FrameSep=\dimen108
)
("C:\Program Files\MiKTeX 2.9\tex\latex\base\alltt.sty"
Package: alltt 1997/06/16 v2.0g defines alltt environment
)
("C:\Program Files\MiKTeX 2.9\tex\latex\upquote\upquote.sty"
Package: upquote 2012/04/19 v1.3 upright-quote and grave-accent glyphs in verba
tim
)

LaTeX Warning: Unused global option(s):
    [table].

(soquestion.aux)
LaTeX Font Info:    Checking defaults for OML/cmm/m/it on input line 52.
LaTeX Font Info:    ... okay on input line 52.
LaTeX Font Info:    Checking defaults for T1/cmr/m/n on input line 52.
LaTeX Font Info:    ... okay on input line 52.
LaTeX Font Info:    Checking defaults for OT1/cmr/m/n on input line 52.
LaTeX Font Info:    ... okay on input line 52.
LaTeX Font Info:    Checking defaults for OMS/cmsy/m/n on input line 52.
LaTeX Font Info:    ... okay on input line 52.
LaTeX Font Info:    Checking defaults for OMX/cmex/m/n on input line 52.
LaTeX Font Info:    ... okay on input line 52.
LaTeX Font Info:    Checking defaults for U/cmr/m/n on input line 52.
LaTeX Font Info:    ... okay on input line 52.
Requested font "cmr12" at 14.4pt
Requested font "cmbx12" at 14.4pt
Requested font "cmbx10" at 10.95pt
Requested font "cmssbx10" at 10.95pt
Requested font "cmss10" at 10.95pt
 [1

]
Overfull \hbox (21.13795pt too wide) in paragraph at lines 62--62
\OT1/cmr/bx/n/14.4 the same start up to Note: They end with clearpage 
 []

[2

] (soquestion.aux) ) 
Here is how much of TeX's memory you used:
 715 strings out of 428783
 8144 string characters out of 3164549
 61307 words of memory out of 3000000
 4071 multiletter control sequences out of 15000+200000
 5452 words of font info for 20 fonts, out of 3000000 for 9000
 1096 hyphenation exceptions out of 8191
 25i,5n,21p,416b,117s stack positions out of 5000i,500n,10000p,200000b,50000s

Output written on soquestion.pdf (2 pages).
  • 2
    Operating system preference? On a Unix-like system, this should be easy with system utilities. – Mike Renfro Feb 2 '16 at 22:00
  • Added Windows 8 to the question. Are you suggesting that I extract the portions I want using regex on the .tex file? I am not sure if that is the right auxiliary file. – lawyeR Feb 2 '16 at 22:31
  • The notes should be in the footer, rather than being forced at the bottom with \clearpage – egreg Feb 2 '16 at 22:34
  • 1
    All of your examples have only one line of text in the subsubsection. Do you want all the text between the \subsubsection and the \clearpage, regardless of how many lines there are? Do you want the \subsubsection command itself, or just the contents of that subsubsection? – Mike Renfro Feb 3 '16 at 1:26
  • 3
    I think it would help if you added an example of the sorts of text (e.g. multiple lines, text containing commands etc) that can appear in your notes together with the corresponding "output file" that is generated. – Andrew Feb 3 '16 at 2:39
2

Since the creation of the file is automated, we can rely on a consistent usage and also use that to our advantage. To that end, I

  • assume you're never using \subsubsection*{..} or \subsubsection[.]{..}

  • assume you'll always have \subsubsection{..} ... \clearpage for any \subsubsection. That is, the text after the \subsubsection title is delimited at the beginning by \subsubsection{..} and at the end by \clearpage.

\documentclass{article}
\usepackage{xcolor}

\let\oldsubsubsection\subsubsection
\makeatletter
\long\def\subsubsection#1#2\clearpage{%
  \begingroup
  \let\textcolor\@secondoftwo% Extract <text> from \textcolor{<color>}{<text>}
  \let\textsf\@firstofone% Ignore \textsf
  \let\textbf\@firstofone% Ignore \textbf
  %\let\par\space% If \par is a problem in the output
  % ... Ignore other commands
  \xdef\@@x{#2}% Extract entire <title> in \subsubsection{<title>}
  \immediate\write\subsubsectiontextfile{\@@x}% Write <title> to file
  \endgroup
  \oldsubsubsection{#1}#2\clearpage% Regular \subsubsection
}
\makeatother
\AtBeginDocument{%
  \newwrite\subsubsectiontextfile
  \immediate\openout\subsubsectiontextfile=\jobname.sst}
\AtEndDocument{\immediate\closeout\subsubsectiontextfile}

\begin{document}

\section{How to Find and Extract Text within Fixed Markers}

\noindent\rule{150pt}{1.7pt}  
\vspace{2pt}
\subsubsection{\textcolor{blue}{\textsf{R} Note: History and Current Status of \textsf{R}}}:
\textsf{R} is an open source software platform used to analyze data.  
Written originally in the early 1990's, it is an \textbf{object-oriented}, \textbf{interpreted} 
programming language blessed with a wealth of constantly-growing and improving "packages" and 
a vibrant user community.

\clearpage

\section{Each Note is at the bottom of a page.  All have the same start up to Note: They end with clearpage}

\noindent\rule{150pt}{1.7pt}  
\vspace{2pt}
\subsubsection{\textcolor{blue}{\textsf{R} Note: Different text}}:  
Some other text. **There are NO OTHER COMMANDS after the colon except \textsf{R} and all of it consists 
of ONE LINE OF TEXT.**

\clearpage

\end{document}

The above produces <jobname>.sst that has in it

: R is an open source software platform used to analyze data. Written originally in the early 1990's, it is an object-oriented, interpreted programming language blessed with a wealth of constantly-growing and improving "packages" and a vibrant user community. \par 
: Some other text. **There are NO OTHER COMMANDS after the colon except R and all of it consists of ONE LINE OF TEXT.** \par 

The part between \begingroup...\endgroup is used to remove all of the possible formatting that might be included in the \subsubsection text. In our case, it removes the functionality of \textcolor{<colour>}{<text>} to \@secondoftwo and \textsf{<text>} and \textbf{<text>} to \@firstofone (in both cases they just return <text>). You can add more of these no-ops here.

Once extracted, we immediately write it to file <jobname>.sst. It's sufficient to open this file \AtBeginDocument and close it \AtEndDocument, as you're not interested in the page its on.

Not sure whether the inclusion of \par is a problem. You could add \let\par\space to convert \par into a regular space .

  • I am awed. It seems petty, but my goal was to get the text AFTER the subsubsection header. In other words, from the closing brace } of the subsubsection to the \clearpage. It seems your code could do that and perhaps be even simpler -- but you are far beyond what I would know how to modify. Assuming you can show how to extract the text (not the subsubsection text after "Note: ") I will be delighted to accept your answer since it is simpler to implement than Perl or Unix regex. – lawyeR Feb 4 '16 at 22:05
  • @lawyeR: I've added an update that grabs content between \subsubsection{..} ... \clearpage. Not sure whether the \par inclusion is a problem. – Werner Feb 5 '16 at 1:54
2

If we take the exact file given in the original question (named, say, myfile.tex), and feed it through sed like so:

sed -n '/\\subsubsection.*Note:/,/\\clearpage/p' myfile.tex

You will get this output:

\subsubsection{\textcolor{blue}{\textsf{R} Note: History and Current Status of \textsf{R}}}:  \textsf{R} is an open source software platform used to analyze data.  Written originally in the early 1990's, it is an \textbf{object-oriented}, \textbf{interpreted} programming language blessed with a wealth of constantly-growing and improving ``packages'' and a vibrant user community.

\clearpage
\subsubsection{\textcolor{blue}{\textsf{R} Note: Different text}}:  Some other text.  **There are NO OTHER COMMANDS after the colon except \textsf{R} and all of it consists of ONE LINE OF TEXT.**

\clearpage

Now, we want to strip out the part up to Note: and the \clearpage, which can be achieved by piping it to:

sed 's/\\subsubsection.*Note: //g;s/\\clearpage//g'

This produces:

 History and Current Status of \textsf{R}}}:  \textsf{R} is an open source software platform used to analyze data.  Written originally in the early 1990's, it is an \textbf{object-oriented}, \textbf{interpreted} programming language blessed with a wealth of constantly-growing and improving ``packages'' and a vibrant user community.


Different text}}:  Some other text.  **There are NO OTHER COMMANDS after the colon except \textsf{R} and all of it consists of ONE LINE OF TEXT.**

Finally, we want to strip the TeX stuff. Piping the result to the command detex (part of TeX Live) will do this.

Putting it all together looks like this:

sed -n '/\\subsubsection.*Note:/,/\\clearpage/p' myfile.tex | sed 's/\\subsubsection.*Note: //g;s/\\clearpage//g' | detex

This results in:

History and Current Status of R:  R is an open source software platform used to analyze data.  Written originally in the early 1990's, it is an object-oriented, interpreted programming language blessed with a wealth of constantly-growing and improving ``packages'' and a vibrant user community.


Different text:  Some other text.  **There are NO OTHER COMMANDS after the colon except R and all of it consists of ONE LINE OF TEXT.**
1

Since it's Windows 8, PowerShell should be included, but I've only got easy access to Windows 10 for testing at the moment. Here's something you can run at a PowerShell prompt, assuming single-line strings will be extracted.

Given a source .tex file of

\documentclass[11pt]{article}     
\begin{document}
\section{How to Find and Extract Text within Fixed Markers}
\noindent\rule{150pt}{1.7pt}  
\vspace{2pt}
\subsubsection{\textcolor{blue}{\textsf{R} Note: History and Current Status of \textsf{R}}}:  \textsf{R} is an open source software platform used to analyze data.  Written originally in the early 1990's, it is an \textbf{object-oriented}, \textbf{interpreted} programming language blessed with a wealth of constantly-growing and improving "packages" and a vibrant user community.
\clearpage
\section{Each Note is at the bottom of a page.  All have the same start up to Note: They end with clearpage}
\noindent\rule{150pt}{1.7pt}  
\vspace{2pt}
\subsubsection{\textcolor{blue}{\textsf{R} Note: Different text}}:  Some other text.
\subsubsection{\textcolor{blue}{\textsf{R} Note: History and Current Status of \textsf{R}}}:  \textsf{R} is an open source software platform used to analyze data.  Written originally in the early 1990's, it is an \textbf{object-oriented}, \textbf{interpreted} programming language blessed with a wealth of constantly-growing and improving "packages" and a vibrant user community.
\clearpage
\end{document}

you can run the following commands at a PowerShell prompt:

sls '^\\subsubsection' 290814.tex `
| select -ExpandProperty line `
| foreach { Write-Host ($_ -split ':  ')[1] }

and get results of

\textsf{R} is an open source software platform used to analyze data.  Written originally in the early 1990's, it is an \textbf{object-oriented}, \textbf{interpreted} programming language blessed with a wealth of constantly-growing and improving "packages" and a vibrant user community.
Some other text.
\textsf{R} is an open source software platform used to analyze data.  Written originally in the early 1990's, it is an \textbf{object-oriented}, \textbf{interpreted} programming language blessed with a wealth of constantly-growing and improving "packages" and a vibrant user community.

I'm not a PowerShell expert by any means, but the simple explanation is:

  • sls is an alias for select-string, roughly equivalent to Unix's grep
  • the select line removes filename and line number prefixes from the resulting lines
  • the foreach iterates over each line of output, splitting the line at : sequences, and returning the second one.

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.