└── BSLLaTeX ├── BSLLaTeX.nb ├── BSLLaTeX ├── Kernel │ └── init.m └── BSLLaTeX.m ├── .WolframResources ├── PacletInfo.m └── .project /BSLLaTeX/BSLLaTeX.nb: -------------------------------------------------------------------------------- 1 | Notebook[{}] -------------------------------------------------------------------------------- /BSLLaTeX/BSLLaTeX/Kernel/init.m: -------------------------------------------------------------------------------- 1 | (* Wolfram Language Init File *) 2 | 3 | Get[ "BSLLaTeX`BSLLaTeX`"] -------------------------------------------------------------------------------- /BSLLaTeX/.WolframResources: -------------------------------------------------------------------------------- 1 | Resources[ 2 | Version[1], 3 | ExecutionBuildCommand["< "BSLLaTeX", 7 | Version -> "0.0.1", 8 | MathematicaVersion -> "11+", 9 | Description -> "Package to help writing LaTeX documents.", 10 | Creator -> "Guido W. Reichert", 11 | Extensions -> 12 | { 13 | {"Documentation", Language -> "English"} 14 | } 15 | ] 16 | 17 | 18 | -------------------------------------------------------------------------------- /BSLLaTeX/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | BSLLaTeX 4 | 5 | 6 | MaTeX 7 | 8 | 9 | 10 | com.wolfram.eclipse.MEET.MathematicaProjectBuilder 11 | 12 | 13 | 14 | 15 | 16 | com.wolfram.eclipse.MEET.SimpleMathematicaNature 17 | 18 | 19 | -------------------------------------------------------------------------------- /BSLLaTeX/BSLLaTeX/BSLLaTeX.m: -------------------------------------------------------------------------------- 1 | (* Wolfram Language Package *) 2 | 3 | (* Created by the Wolfram Workbench 27.04.2017 *) 4 | 5 | (* :Title: BSLLaTeX *) 6 | (* :Author: Guido Wolf Reichert *) 7 | (* :Context: BSLLaTeX` *) 8 | (* :Version: 0.0.1 *) 9 | (* :Date: 2017-04-27 *) 10 | (* :Mathematica Version: 11.1 *) 11 | (* :Copyright: (c) 2017 Guido W. Reichert *) 12 | 13 | BeginPackage["BSLLaTeX`", { "MaTeX`" }] (* MaTeX may not be really needed, keeping making this independent *) 14 | (* Exported symbols added here with SymbolName::usage *) 15 | 16 | LaTeXFigure::usage = "\ 17 | LaTeXFigure[file, caption, shortcap] generates the code for a figure environent." 18 | 19 | LaTeXTable::usage = "\ 20 | LaTeXTable[content, caption, shortcap] generates the code for a table environment." 21 | 22 | $LaTeXRules::usage = "\ 23 | $LaTeXRules give some convenient string replacement rules that allow for an immediate use of text input." 24 | 25 | LaTeXTabulary::usage = "\ 26 | LaTeXTabulary[assoc] \n\ 27 | LaTeXTabulary[{assoc1,\[TripleDot]}] \n\ 28 | LaTeXTabulary[\[LeftAssociation]key1 \[Rule] assoc1, \[TripleDot] \[RightAssociation]] create a tabulary environment." 29 | 30 | LaTeXTableForm::usage = "\ 31 | LaTeXTableForm[strArray] will return the array of strings in a form that can be immediately plugged into a tabular environment." 32 | 33 | LaTeXCompile::usage = "\ 34 | LaTeXCompile[filename] runs pdflatex to compile the texfile." 35 | 36 | Slots::usage = "\ 37 | Slots[template] will return the named slots of a template object." 38 | 39 | Begin["`Private`"] 40 | (* Implementation of the package *) 41 | 42 | $CharacterEncoding = "UTF8" 43 | 44 | $LaTeXRules = { 45 | FromCharacterCode[8222] -> "\\glqq ", 46 | FromCharacterCode[8220] -> "\\grqq{}", 47 | "%" -> "\\%", 48 | "#" -> "\\#", 49 | "&" -> "\\&", 50 | "_" -> "\\_", 51 | " x " -> " $\\times$ ", 52 | "->" -> "$\\rightarrow$", 53 | "=>" -> "$\\implies$" 54 | } 55 | 56 | (* expand TemplateObject *) 57 | Unprotect[ System`TemplateObject ] 58 | 59 | System`TemplateObject/: Slots[ template_System`TemplateObject ] := Cases[ template, TemplateSlot[ name_ ] :> name, Infinity ] 60 | 61 | Protect[ System`TemplateObject ] 62 | 63 | Options[ LaTeXFigure ] = { 64 | "FigurePlacement" -> "htb", 65 | "FigureWidth" -> "0.9 \\textwidth", 66 | "Interpolate" -> True, (* useful for bitmaps to prevent pixels from showing *) 67 | "Prefix" -> None, 68 | "Suffix" -> None 69 | } 70 | 71 | LaTeXFigure[ file_String, caption_String, shortcap_String, opts:OptionsPattern[] ] := With[ 72 | { 73 | parts = Association[ 74 | "placement" -> OptionValue[ "FigurePlacement" ], 75 | "width" -> OptionValue[ "FigureWidth" ], 76 | "file" -> file, 77 | "shortcaption" -> shortcap, 78 | "caption" -> caption, 79 | "interpolate" -> If[ OptionValue[ "Interpolate" ] === True, ", interpolate = true", "" ], 80 | "prefix" -> Switch[ OptionValue[ "Prefix" ], _String , OptionValue[ "Prefix" ], _ , "" ], 81 | "suffix" -> Switch[ OptionValue[ "Suffix" ], _String , OptionValue[ "Suffix" ], _ , "" ] 82 | ] 83 | }, 84 | TemplateApply[ 85 | StringJoin[ 86 | "`prefix`\n", 87 | "\\begin{figure}[`placement`]\n", 88 | "\\centering\n", 89 | "\\includegraphics[width = `width``interpolate`]{`file`}\n", 90 | "\\caption[`shortcaption`]{`caption`}\n", 91 | "\\label{fig:`shortcaption`}\n", 92 | "\\end{figure}\n", 93 | "`suffix`\n\n" 94 | ], 95 | parts 96 | ] 97 | ] 98 | 99 | LaTeXFigure[ ___ ] := Return[ Message[ LaTeXFigure::fargs ] ; $Failed ] 100 | 101 | Options[ LaTeXTable ] = { 102 | "TablePlacement" -> "htb", 103 | "Prefix" -> None, 104 | "Suffix" -> None 105 | } 106 | 107 | LaTeXTable[ content_String, caption_String, shortcap_String, opts:OptionsPattern[] ] := With[ 108 | { 109 | parts = Association[ 110 | "placement" -> OptionValue[ "TablePlacement" ], 111 | "content" -> content, 112 | "shortcaption" -> shortcap, 113 | "caption" -> caption, 114 | "prefix" -> Switch[ OptionValue[ "Prefix" ], _String , OptionValue[ "Prefix" ], _ , "" ], 115 | "suffix" -> Switch[ OptionValue[ "Suffix" ], _String , OptionValue[ "Suffix" ], _ , "" ] 116 | ] 117 | }, 118 | TemplateApply[ 119 | StringJoin[ 120 | "`prefix`\n", 121 | "\\begin{table}[`placement`]\n", 122 | "\\centering\n", 123 | "\\caption[`shortcaption`]{`caption`}\n", 124 | "\\label{tab:`shortcaption`}\n", 125 | "`content`", 126 | "\\end{table}\n", 127 | "`suffix`\n\n" 128 | ], 129 | parts 130 | ] 131 | ] 132 | 133 | LaTeXTable[ ___ ] := Return[ Message[ LaTeXTable::fargs ] ; $Failed ] 134 | 135 | Options[ LaTeXTableForm ] = { 136 | "StringStyle" -> "`1`" (* use placeholder `` to indicate any string, e.g. "\\textbf{`1`}" *) 137 | } 138 | 139 | LaTeXTableForm[ array_, opts:OptionsPattern[] ] /; ArrayQ[ array, 2 , StringQ ] := With[ 140 | { 141 | formattedArray = Map[ 142 | TemplateApply[ 143 | OptionValue[ "StringStyle" ], 144 | { # } 145 | ]&, 146 | array, 147 | {2} 148 | ] 149 | }, 150 | StringJoin[ 151 | StringRiffle[ formattedArray, " \\\\ \n", " & " ], 152 | " \\\\ \n" 153 | ] 154 | ] 155 | 156 | LaTeXTableForm[ assoc_Association, opts:OptionsPattern[] ] := LaTeXTableForm[ Values @ { assoc }, opts ] 157 | 158 | LaTeXTableForm[ list_, opts:OptionsPattern[] ] /; VectorQ[ list, StringQ ] := LaTeXTableForm[ { list }, opts ] 159 | 160 | LaTeXTableForm[ ___ ] := Return[ Message[ LaTeXTableForm::fargs ] ; $Failed ] 161 | 162 | 163 | 164 | Options[ LaTeXTabulary ] = { 165 | "TableSpec" -> Automatic , (* or a sequence of LCRJ-alignment letters *) 166 | "TableWidth" -> "0.9\\textwidth" , 167 | "MinimumLength" -> None , (* \tymin *) 168 | "MaximumLength" -> None , (* \tymax *) 169 | "HeaderStyle" -> "\\textbf{`1`}", 170 | "BodyStyle" -> "`1`" 171 | } 172 | 173 | LaTeXTabulary[ assoc : { __Association }, opts:OptionsPattern[] ] /; ArrayQ @ assoc := With[ 174 | { 175 | keys = LaTeXTableForm[ Keys @ assoc[[1]], "StringStyle" -> OptionValue[ "HeaderStyle" ] ], 176 | values = LaTeXTableForm[ Values @ assoc , "StringStyle" -> OptionValue[ "BodyStyle" ] ], 177 | 178 | (* make left alignment a default *) 179 | tableSpec = Switch[ OptionValue["TableSpec"], 180 | _String, 181 | OptionValue["TableSpec"], 182 | _ , 183 | ConstantArray[ "L", Length @ First @ assoc ] // StringJoin 184 | ], 185 | tableWidth = OptionValue[ "TableWidth" ], 186 | tymin = Switch[ OptionValue[ "MinimumLength" ], 187 | _String, 188 | "\\tymin = " <> OptionValue[ "MinimumLength" ] <> "\n", 189 | _ , 190 | "" 191 | ], 192 | tymax = Switch[ OptionValue[ "MaximumLength" ], 193 | _String, 194 | "\\tymax = " <> OptionValue[ "MaximumLength" ] <> "\n", 195 | _ , 196 | "" 197 | ] 198 | }, 199 | Module[ 200 | { 201 | parts = Association[ 202 | "tablewidth" -> tableWidth , 203 | "tablespec" -> tableSpec , 204 | "tymin" -> tymin, 205 | "tymax" -> tymax, 206 | "header" -> keys, 207 | "body" -> values 208 | ] 209 | }, 210 | TemplateApply[ 211 | StringJoin[ 212 | "`tymin`", 213 | "`tymax`", 214 | "\\begin{tabulary}{`tablewidth`}{`tablespec`}\n", 215 | "\\toprule\n", 216 | "`header`", 217 | "\\midrule\n", 218 | "`body`", 219 | "\\bottomrule\n", 220 | "\\end{tabulary}\n" 221 | ], 222 | parts 223 | ] 224 | ] 225 | ] 226 | 227 | LaTeXTabulary[ assoc_Association, opts:OptionsPattern[] ] := LaTeXTabulary[ { assoc }, opts ] 228 | 229 | LaTeXTabulary[ ___ ] := Return[ Message[ LaTeXTabulary::fargs ] ; $Failed ] 230 | 231 | LaTeXCompile[ file_String ] := Module[ 232 | { 233 | resetDirectoryQ = True, 234 | fileName = FileNameTake @ file, 235 | directoryName = DirectoryName @ file 236 | }, 237 | 238 | (* check whether the directory needs to be changed *) 239 | Which[ 240 | directoryName === "" && FileExistsQ @ fileName , 241 | resetDirectoryQ = False, 242 | FileExistsQ @ file == False, 243 | Return[ Message[ LaTeXCompile::fdnfnd, file ]; $Failed ], 244 | True, 245 | resetDirectoryQ = True; 246 | SetDirectory[ directoryName ] 247 | ]; 248 | 249 | (* run pdflatex - assuming that it has been installed properly for now *) 250 | Run[ "pdflatex " <> fileName ]; 251 | 252 | (* reset the directory if needed *) 253 | If[ resetDirectoryQ, 254 | (* then *) 255 | ResetDirectory[] 256 | ]; 257 | 258 | Print @ StringJoin[ 259 | FileBaseName @ file, 260 | ".pdf" 261 | ] 262 | ] 263 | 264 | LaTeXCompile[ ___ ] := Return[ Message[ LaTeXCompile::fargs ] ; $Failed ] 265 | 266 | End[] 267 | 268 | EndPackage[] 269 | 270 | --------------------------------------------------------------------------------