1######################################################################## 2## 3## Copyright (C) 2016-2021 The Octave Project Developers 4## 5## See the file COPYRIGHT.md in the top-level directory of this 6## distribution or <https://octave.org/copyright/>. 7## 8## This file is part of Octave. 9## 10## Octave is free software: you can redistribute it and/or modify it 11## under the terms of the GNU General Public License as published by 12## the Free Software Foundation, either version 3 of the License, or 13## (at your option) any later version. 14## 15## Octave is distributed in the hope that it will be useful, but 16## WITHOUT ANY WARRANTY; without even the implied warranty of 17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18## GNU General Public License for more details. 19## 20## You should have received a copy of the GNU General Public License 21## along with Octave; see the file COPYING. If not, see 22## <https://www.gnu.org/licenses/>. 23## 24######################################################################## 25 26## -*- texinfo -*- 27## @deftypefn {} {@var{outstr} =} __publish_latex_output__ (@var{type}, @var{varargin}) 28## 29## Internal function. 30## 31## The first input argument @var{type} defines the required strings 32## (@samp{str}) or cell-strings (@samp{cstr}) in @var{varargin} in order 33## to produce @LaTeX{} output. 34## 35## @var{type} is one of 36## 37## @itemize @bullet 38## @item 39## @samp{output_file_extension} () 40## 41## @item 42## @samp{header} (title_str, intro_str, toc_cstr) 43## 44## @item 45## @samp{footer} () 46## 47## @item 48## @samp{code} (str) 49## 50## @item 51## @samp{code_output} (str) 52## 53## @item 54## @samp{section} (str) 55## 56## @item 57## @samp{preformatted_code} (str) 58## 59## @item 60## @samp{preformatted_text} (str) 61## 62## @item 63## @samp{bulleted_list} (cstr) 64## 65## @item 66## @samp{numbered_list} (cstr) 67## 68## @item 69## @samp{graphic} (str) 70## 71## @item 72## @samp{html} (str) 73## 74## @item 75## @samp{latex} (str) 76## 77## @item 78## @samp{text} (str) 79## 80## @item 81## @samp{blockmath} (str) 82## 83## @item 84## @samp{inlinemath} (str) 85## 86## @item 87## @samp{bold} (str) 88## 89## @item 90## @samp{italic} (str) 91## 92## @item 93## @samp{monospaced} (str) 94## 95## @item 96## @samp{link} (url_str, url_str, str) 97## 98## @item 99## @samp{TM} () 100## 101## @item 102## @samp{R} () 103## 104## @item 105## @samp{escape_special_chars} (str) 106## @end itemize 107## @end deftypefn 108 109function outstr = __publish_latex_output__ (type, varargin) 110 outstr = feval (["do_" type], varargin{:}); 111endfunction 112 113function outstr = do_output_file_extension () 114 outstr = ".tex"; 115endfunction 116 117function outstr = do_header (title_str, intro_str, toc_cstr) 118 publish_comment = sprintf ("%s\n", 119"", 120"", 121"% This document was generated by the publish-function", 122["% from GNU Octave " version()], 123""); 124 125 latex_preamble = sprintf ("%s\n", 126"", 127"", 128'\documentclass[10pt]{article}', 129'\usepackage{listings}', 130'\usepackage{mathtools}', 131'\usepackage{amssymb}', 132'\usepackage{graphicx}', 133'\usepackage{hyperref}', 134'\usepackage{xcolor}', 135'\usepackage{titlesec}', 136'\usepackage[utf8]{inputenc}', 137'\usepackage[T1]{fontenc}', 138'\usepackage{lmodern}'); 139 140 listings_option = sprintf ("%s\n", 141"", 142"", 143'\lstset{', 144'language=Octave,', 145'numbers=none,', 146'frame=single,', 147'tabsize=2,', 148'showstringspaces=false,', 149'breaklines=true}'); 150 151 latex_head = sprintf ("%s\n", 152"", 153"", 154'\titleformat*{\section}{\Huge\bfseries}', 155'\titleformat*{\subsection}{\large\bfseries}', 156'\renewcommand{\contentsname}{\Large\bfseries Contents}', 157'\setlength{\parindent}{0pt}', 158"", 159'\begin{document}', 160"", 161['{\Huge\section*{' title_str '}}'], 162"", 163'\tableofcontents', 164'\vspace*{4em}', 165""); 166 167 outstr = [publish_comment, latex_preamble, listings_option, latex_head]; 168 169endfunction 170 171function outstr = do_footer (m_source_str) 172 outstr = ["\n\n" '\end{document}' "\n"]; 173endfunction 174 175function outstr = do_code (str) 176 outstr = ['\begin{lstlisting}' "\n", str, "\n" '\end{lstlisting}' "\n"]; 177endfunction 178 179function outstr = do_code_output (str) 180 outstr = sprintf ("%s\n", 181'\begin{lstlisting}[language={},xleftmargin=5pt,frame=none]', 182str, 183'\end{lstlisting}'); 184endfunction 185 186function outstr = do_section (str) 187 outstr = sprintf ("%s\n", 188"", 189"", 190'\phantomsection', 191['\addcontentsline{toc}{section}{' str '}'], 192['\subsection*{' str '}'], 193""); 194endfunction 195 196function outstr = do_preformatted_code (str) 197 outstr = sprintf ("%s\n", 198'\begin{lstlisting}', 199str, 200'\end{lstlisting}'); 201endfunction 202 203function outstr = do_preformatted_text (str) 204 outstr = sprintf ("%s\n", 205'\begin{lstlisting}[language={}]', 206str, 207'\end{lstlisting}'); 208endfunction 209 210function outstr = do_bulleted_list (cstr) 211 outstr = ["\n" '\begin{itemize}' "\n"]; 212 for i = 1:numel (cstr) 213 outstr = [outstr, '\item ' cstr{i} "\n"]; 214 endfor 215 outstr = [outstr, '\end{itemize}' "\n"]; 216endfunction 217 218function outstr = do_numbered_list (cstr) 219 outstr = ["\n" '\begin{enumerate}' "\n"]; 220 for i = 1:numel (cstr) 221 outstr = [outstr, '\item ' cstr{i} "\n"]; 222 endfor 223 outstr = [outstr, "\\end{enumerate}\n"]; 224endfunction 225 226function outstr = do_graphic (str) 227 outstr = sprintf ("%s\n", 228'\begin{figure}[!ht]', 229['\includegraphics[width=\textwidth]{' str '}'], 230'\end{figure}'); 231endfunction 232 233function outstr = do_html (str) 234 outstr = ""; 235endfunction 236 237function outstr = do_latex (str) 238 outstr = ["\n" str "\n"]; 239endfunction 240 241function outstr = do_link (url_str, str) 242 outstr = ['\href{' url_str '}{' str '}']; 243endfunction 244 245function outstr = do_text (str) 246 outstr = ["\n\n" str "\n\n"]; 247endfunction 248 249function outstr = do_blockmath (str) 250 outstr = ["$$" str "$$"]; 251endfunction 252 253function outstr = do_inlinemath (str) 254 outstr = ["$" str "$"]; 255endfunction 256 257function outstr = do_bold (str) 258 outstr = ['\textbf{' str '}']; 259endfunction 260 261function outstr = do_italic (str) 262 outstr = ['\textit{' str '}']; 263endfunction 264 265function outstr = do_monospaced (str) 266 outstr = ['\texttt{' str '}']; 267endfunction 268 269function outstr = do_TM () 270 outstr = '\texttrademark '; 271endfunction 272 273function outstr = do_R () 274 outstr = '\textregistered '; 275endfunction 276 277function str = do_escape_special_chars (str) 278 ## Escape \, {, }, &, %, #, _, ~, ^, <, > 279 str = regexprep (str, '\\', "\\ensuremath{\\backslash}"); 280 str = regexprep (str, '(?<!\\)(\{|\}|&|%|#|_)', '\\$1'); 281 ## Revert accidental {} replacements for backslashes 282 str = strrep (str, '\ensuremath\{\backslash\}', '\ensuremath{\backslash}'); 283 str = regexprep (str, '(?<!\\)~', "\\ensuremath{\\tilde{\\;}}"); 284 str = regexprep (str, '(?<!\\)\^', "\\^{}"); 285 str = regexprep (str, '(?<!\\)<', "\\ensuremath{<}"); 286 str = regexprep (str, '(?<!\\)>', "\\ensuremath{>}"); 287endfunction 288