\% Convert Matlab lessons published to latex to Octave form.

\% Usage: subbin.exe frommatlab.sub lessonNorg.tex lessonN.tex

\% Notes:
\%
\% 1) Matlab does not recognize all inline math.  This script will try
\% to correct.  To minimize conversion problems, make sure that inline
\% math does not contain any whitespace.
\%
\% 2) Matlab does not do <...> links; for <URL> it shows URL in
\% verbatim and for <URL TAG> it shows TAG in verbatim.  <URL> links
\% where URL starts with http will be fixed up.  <URL TAG> links can
\% obviously not be fixed, as the URL is gone.  You may want to put
\% the href in a <latex> environment or edit lessonN.tex when
\% otherwise OK.

\% A backslash starts commands in this file.  For a literal backslash,
\% double it.  The file format is:
\%
\%    ORIGINAL_TEXT
\%    REPLACEMENT_TEXT
\%
\%    ...

\% remove any control characters we want to use as markers
\(\ -\F\K\L\N-\Z\)
\%



\% CONVERT HEADER

\\documentclass{article}
\\documentclass[10pt]{article}

\\usepackage{graphicx}\?\J
\%

\\usepackage{color}
\\usepackage{listings}\J\\usepackage{mathtools}\J\\usepackage{amssymb}\J\\usepackage{graphicx}\J\\usepackage{hyperref}\J\\usepackage{xcolor}\J\\usepackage{titlesec}\J\\usepackage[utf8]{inputenc}\J\\usepackage[T1]{fontenc}\J\\usepackage{lmodern}\J\J\J\\lstset{\Jlanguage=Octave,\Jnumbers=none,\Jframe=single,\Jtabsize=2,\Jshowstringspaces=false,\Jbreaklines=true}\J\J\J\\titleformat*{\\section}{\\Huge\\bfseries}\J\\titleformat*{\\subsection}{\\large\\bfseries}\J\\renewcommand{\\contentsname}{\\Large\\bfseries Contents}\J\A

\\sloppy
\%

\\definecolor{lightgray}{gray}{0.5}
\%

\A\?\+
\%

\\begin{document}\?\+
\\begin{document}\J\J



\% CONVERT SECTIONS AND CONTENTS

\% convert the title
\\section*{\{\?\~\({}\J\)\}}
{\\Huge\\\Asection*{\#1}}
\\section*{\?\~\J
\!1:No Title!
\A\{\?\~\(\J\)\J\}
\#1\A

\% convert the table of contents
\\end{itemize}
\#0\B
\\subsection*{Contents}\?\+\\begin{itemize}\?\~\B\B
\C\\tableofcontents\J\\vspace*{4em}\X
\\subsection*{Contents}
\!1:No Contents!
\A\?\~\(\A\C\)\C
\J
\(\A-\C\)
\%

\% convert the section headers
\\subsection*{\{\?\~\({}\J\)\}}
\\phantomsection\J\\addcontentsline{toc}{section}{\#1}\J\\subsection*{\#1}
\J\?\_\J\?\_\\subsection*{\?\~\J
\!1:Unprocessed Section!



\% PARAGRAPHS AND ENVIRONMENTS

\% remove \begin{par}
\_\?\_\\begin{par}\?\_
\%
\\begin{par}\?\_
\%

\% make \\end{par} into a blank line
\J\?\_\\end{par}\?\+\\vspace{1em}
\J\J\\vspace{0.83\\baselineskip}
\J\?\_\\end{par}\?\_
\J\J
\\end{par}
\!1:Bad end{par}

\% spacing
\\vspace{1em}
\\vspace{0.83\\baselineskip}

\% mark verbatim tags
\\begin{verbatim}
\A\#0
\\end{verbatim}
\B\#0

\% fix URLs without titles (cannot fix with titles)
\A\\begin{verbatim}http\?\~\(\B\J\_\)\B\\end{verbatim}
\r%\\%
\A\\begin{verbatim}\{http\?\~\(\B\J\_\)\}\B\\end{verbatim}
\\href{\#1}{\#1}

\% remove leading whitespace from the tags
\J\?\_\{\(\A\B\)\}
\J\#1

\% start a new line before the tags if none
\J\(\A\B\)
\J
\(\A\B\)
\J

\% start a new line behind \begin{verbatim} if none
\\begin{verbatim}\{\~\J\}
\\begin{verbatim}\J\#1

\% convert \begin{verbatim}
\J\?\_\\color{lightgray}\?\_\J\\begin{verbatim}
\J\\begin{lstlisting}[language={},xleftmargin=5pt,frame=none]
\\begin{verbatim}
\\begin{lstlisting}

\% convert \end{verbatim}
\\end{verbatim} \\color{black}
\\end{lstlisting}
\\end{verbatim}
\\end{lstlisting}

\% take out excessive vertical space
\\vspace{0.83\\baselineskip}\{\?\+\\begin{\}
\#1
\\vspace{0.83\\baselineskip}\{\?\+$$\}
\#1
\{\~\($\\\)$$\?\+\}\\vspace{0.83\\baselineskip}
\#1

\% COMMANDS AND CHARACTERS

\% we do not need \` in matlab
\\ensuremath{\\backslash}`
\%

\% hide literal dollar signs
\\$\?\~\J
\?1:Warning: Unrecognized inline math??  May try to restore.
\\$
\Y

\% hide backslashes in math
\{\~\($\\\)\}$$
\#1\A
\A\?\~\A\A
\r\\\Z
$\?\~\(\+$\)$
\r\\\Z
\A
$$

\% hide backslashes in our starting block
% make some code and output formatting changes
\A\#0
\\def\\&{\\ifmmode&\\else\\symbol{38}\\fi}
\#0\A
\A\?\~\A\A
\r\\\Z
\A
\%

\% restore active curly braces
\\\\
\A
\\{
{
\\}
}
\A
\\\\

\% restore active backslashes
\\ensuremath{\\backslash}
\\

\% try to fix unrecognized inline math
\Y\?\~\(\+\Y$\)\Y
\r\Y$



\% FINAL FIXES

\% whitespace after the table of contents
\X\?\+
\J\J

\% restore literal dollar
\Y
\\$

\% restore backslash in math
\Z
\\
