├── .github └── workflows │ ├── emacs-tests.yml │ └── melpazoid.yml ├── LICENSE ├── README.org ├── images └── litex.png ├── litex-mode.el ├── litex-mode.texi └── tests └── litex-mode-tests.el /.github/workflows/emacs-tests.yml: -------------------------------------------------------------------------------- 1 | # ert tests 2 | 3 | name: ert 4 | on: [push, pull_request] 5 | 6 | jobs: 7 | build: 8 | runs-on: ubuntu-latest 9 | steps: 10 | - uses: actions/checkout@v2 11 | - name: Install 12 | run: | 13 | python -m pip install --upgrade pip 14 | sudo apt-get install emacs && emacs --version 15 | - name: Checkout units-mode 16 | uses: actions/checkout@v2 17 | with: 18 | repository: atreyagaurav/units-mode 19 | path: units-mode 20 | - name: Run Tests 21 | run: emacs -batch -l ./units-mode/units-mode.el -l ./litex-mode.el -l ert -l ./tests/litex-mode-tests.el -f ert-run-tests-batch-and-exit 22 | -------------------------------------------------------------------------------- /.github/workflows/melpazoid.yml: -------------------------------------------------------------------------------- 1 | # melpazoid build checks. 2 | 3 | # If your package is on GitHub, enable melpazoid's checks by copying this file 4 | # to .github/workflows/melpazoid.yml and modifying RECIPE and EXIST_OK below. 5 | 6 | name: melpazoid 7 | on: [push, pull_request] 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v2 14 | - name: Set up Python 3.6 15 | uses: actions/setup-python@v1 16 | with: { python-version: 3.6 } 17 | - name: Install 18 | run: | 19 | python -m pip install --upgrade pip 20 | sudo apt-get install emacs && emacs --version 21 | git clone https://github.com/riscy/melpazoid.git ~/melpazoid 22 | pip install ~/melpazoid 23 | - name: Run 24 | env: 25 | LOCAL_REPO: ${{ github.workspace }} 26 | # RECIPE is your recipe as written for MELPA: 27 | RECIPE: (litex-mode :repo "atreyagaurav/litex-mode" :fetcher github) 28 | # set this to false (or remove it) if the package isn't on MELPA: 29 | EXIST_OK: true 30 | run: echo $GITHUB_REF && make -C ~/melpazoid 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.org: -------------------------------------------------------------------------------- 1 | [[https://melpa.org/#/litex-mode][file:https://melpa.org/packages/litex-mode-badge.svg]] 2 | [[https://stable.melpa.org/#/litex-mode][file:https://stable.melpa.org/packages/litex-mode-badge.svg]] 3 | 4 | * LiTeX mode 5 | This is a minor mode to convert valid elisp/lisp expressions to latex. 6 | 7 | This is useful for emacs users because emacs allows lisp code to be directly evaluated inside any buffer, which means no need to have any code blocks. But since lisp expressions aren't popular we can't expect to put them in reports, so to overcome that I wrote this mode with the help of my friend [[https://gist.github.com/bpanthi977][Bibek Panthi]]. 8 | 9 | The beginning of this mode, and previous codes are here: 10 | - [[https://emacs.stackexchange.com/questions/70358/how-can-i-convert-a-lisp-expression-to-a-latex-math-expression/70360?noredirect=1#comment113158_70360][Stackexchange Question]] 11 | - [[https://gist.github.com/bpanthi977/4b8ece0eeff3bc05bb82275a23cbb56d][Bibek's gist]] 12 | - [[https://github.com/Atreyagaurav/emacs-modules][My previous calculation template mode and first few versions of LiTeX]] 13 | 14 | For example if you look at this image you can see how just 1 lines of lisp expression can be converted to the final result of several lines of mathematical expression without even having to do any of the calculation. 15 | 16 | [[./images/litex.png]] 17 | 18 | Not only can it convert lisp expressions to latex, it can also, give intermediate solution steps. Perfect for doing homeworks (as that's what I made it for) :P 19 | 20 | 21 | * Table of contents :TOC: 22 | - [[#litex-mode][LiTeX mode]] 23 | - [[#how-to-use][How to Use]] 24 | - [[#installation][Installation]] 25 | - [[#usage][Usage]] 26 | - [[#keybindings][Keybindings]] 27 | - [[#setting-keybindings-for-individual-functions][Setting keybindings for individual functions]] 28 | - [[#setting-the-whole-litex-provided-keybindings-to-a-prefix-key][Setting the whole litex provided keybindings to a prefix key]] 29 | - [[#complete-setup-using-use-package][Complete setup using use-package]] 30 | - [[#known-problems][Known Problems:]] 31 | - [[#tips-and-tricks][Tips and Tricks]] 32 | - [[#including-units][Including Units]] 33 | - [[#using-case-sensitive-symbols][Using case sensitive symbols]] 34 | - [[#slime-integration][Slime integration]] 35 | - [[#using-litex-variables-in-your-normal-calculations][Using LiTeX variables in your normal calculations]] 36 | - [[#using-greek-letters][Using Greek letters]] 37 | - [[#explanation-for-functions][Explanation for functions]] 38 | - [[#litex-format-region-last][litex-format-region-last]] 39 | - [[#litex-format-region][litex-format-region]] 40 | - [[#litex-eval-and-replace][litex-eval-and-replace]] 41 | - [[#litex-eval-and-insert][litex-eval-and-insert]] 42 | - [[#litex-sexp-to-latex-exp][litex-sexp-to-latex-exp]] 43 | - [[#litex-sexp-solve-all-steps][litex-sexp-solve-all-steps]] 44 | - [[#litex-increment-number][litex-increment-number]] 45 | - [[#litex-exp-to-latex][litex-exp-to-latex]] 46 | - [[#litex-exp-in-latex-math][litex-exp-in-latex-math]] 47 | - [[#litex-sexp-solve-all-steps-equation][litex-sexp-solve-all-steps-equation]] 48 | - [[#litex-sexp-solve-all-steps-eqnarray][litex-sexp-solve-all-steps-eqnarray]] 49 | - [[#customization][Customization]] 50 | - [[#contributing][Contributing]] 51 | 52 | * How to Use 53 | 54 | Here is a [[https://youtu.be/esEf63gSSPg][demo video]] describing the features of LiTeX mode. 55 | 56 | For Text descriptions refer sections below for details on what each function does, and the meaning of custom variables. 57 | 58 | You can also access it all from texinfo file provided with the package. 59 | 60 | * Installation 61 | You can install it through melpa with ~M-x package-install litex-mode ~. 62 | 63 | Or you can clone this github repo and load it using path. 64 | 65 | Configuration section shows how you can add the load path using ~use-package~. 66 | 67 | * Usage 68 | Using ~use-package~ you can do: 69 | 70 | #+begin_src emacs-lisp :tangle yes 71 | (use-package litex-mode 72 | ;; :load-path "/path/to/litex-mode/" 73 | :commands litex-mode 74 | :hook text-mode) 75 | #+end_src 76 | 77 | Please look through the Keybindings, Functions, Variables, Known problems and the Tips and Tricks to get 100% out of this package. 78 | 79 | * Keybindings 80 | By default LiTeX-mode doesn't provide any keybindings, but it does have a variable containing bindings for the interactive functions that you can use. 81 | 82 | 83 | ** Setting keybindings for individual functions 84 | You can use ~local-set-key~ to bind individual functions to a key binding. 85 | #+begin_src emacs-lisp :tangle yes 86 | (local-set-key (kbd "×") 'litex-insert-or-replace-x) 87 | #+end_src 88 | 89 | ** Setting the whole litex provided keybindings to a prefix key 90 | 91 | You can set a prefix key (~C-e~ for me here) like in this example, which makes it so you can for example use ~litex-format-region-last~ using ~C-e f~ using the following in your config. In some cases you might have to unset key ~C-e~ because it's used to goto end of line, I'm replacing that because I don't use it (as ~End~ key does the same). 92 | #+begin_src emacs-lisp :tangle yes 93 | (local-set-key (kbd "C-e") litex-key-map) 94 | #+end_src 95 | 96 | 97 | Contents of litex-key-map are below. 98 | 99 | #+begin_src emacs-lisp :tangle yes 100 | (define-key litex-key-map (kbd "F") 'litex-format-region) 101 | (define-key litex-key-map (kbd "f") 'litex-format-region-last) 102 | (define-key litex-key-map (kbd "E") 'litex-eval-and-replace) 103 | (define-key litex-key-map (kbd "e") 'litex-eval-and-insert) 104 | (define-key litex-key-map (kbd "s") 'litex-sexp-to-latex-exp) 105 | (define-key litex-key-map (kbd "S") 'litex-sexp-solve-all-steps) 106 | (define-key litex-key-map (kbd "+") 'litex-increment-number) 107 | (define-key litex-key-map (kbd "l") 'litex-exp-to-latex) 108 | (define-key litex-key-map (kbd "m") 'litex-exp-in-latex-math) 109 | (define-key litex-key-map (kbd "A") 'litex-sexp-solve-all-steps-equation) 110 | (define-key litex-key-map (kbd "a") 'litex-sexp-solve-all-steps-eqnarray) 111 | #+end_src 112 | 113 | 114 | ** Complete setup using use-package 115 | This is the complete setup using use-package, if you installed from melpa. If you installed by cloning the repo, uncomment and provide the load path. 116 | #+begin_src emacs-lisp :tangle yes 117 | (use-package litex-mode 118 | ;; :load-path "/path/to/litex-mode/" 119 | :commands litex-mode 120 | :hook text-mode 121 | :config 122 | (local-set-key (kbd "C-e") litex-key-map) 123 | (local-set-key (kbd "×") 'litex-insert-or-replace-x)) 124 | #+end_src 125 | 126 | 127 | * Known Problems: 128 | elisp uses integer calculations so ~(/ 1 2)~ is evaluated to 0, be careful of such pitfalls. For now ~(/ 1.0 2)~ is evaluated as ~0.5~, so I'd recommend using floats when you need floats. 129 | 130 | This problem doesn't exist if you use slime integration. But slime will evaluate =(/ 1 2)= as =1/2= so expect that to be the final answer. 131 | 132 | * Tips and Tricks 133 | 134 | ** Including Units 135 | LiTeX mode now supports unit conversion and formatting using the =[[https://github.com/Atreyagaurav/units-mode][units-mode]]= which in turn uses gnu units. 136 | 137 | The formatting is like below, the functions are from =units-mode= package. 138 | - =(units-convert-simple (/ 1 2) "m" "ft") ⇒ \unit[\frac{1}{2}]{m}= 139 | - =(units-ignore 5 "ft") ⇒ \unit[5]{ft}= 140 | 141 | It also formats correctly on step by step solve, for example: 142 | - =(setq L (units-convert-simple (+ 1 2) "m" "ft"))= 143 | 144 | #+begin_src latex 145 | \begin{align*} 146 | L& = \unit[1 + 2]{m}\\ 147 | & = \unit[3]{m}\\ 148 | & = \unit[9.843]{ft} 149 | \end{align*} 150 | #+end_src 151 | 152 | - =(setq l (units-ignore 24 "in"))= 153 | 154 | #+begin_src latex 155 | \begin{align*} 156 | l& = \unit[24]{in} 157 | \end{align*} 158 | #+end_src 159 | 160 | *Note that the =\unit= latex command is from [[https://mirrors.mit.edu/CTAN/macros/latex/contrib/units/units.pdf][=units= package]], so you have to include it in your preamble.* 161 | 162 | If you're using slime for evaluation, you need to load the units functions in slime too. The functions are available in units-mode's github named =clisp/units.lisp=. 163 | 164 | For =sbcl= you can put the contents in =clisp/units.lisp= to =~/.sbclrc= so it's evaluated in sbcl/slime startup. 165 | 166 | ** Using case sensitive symbols 167 | Inside emacs the symbols are read without case sensitivity, so if you define and variable names ~ABCD~, it'll replace the variable named ~abcd~. To avoid that, specially if you have formula with both lowercase and uppercase symbols you can use this customization. 168 | 169 | #+begin_src emacs-lisp :tangle yes 170 | (setq readtable-case :preserve) 171 | #+end_src 172 | 173 | NOTE: Currently it only works for elisp, and not for slime integration, I'm searching for a solution with slime. 174 | 175 | ** Slime integration 176 | If you want to do the calculations in your favorite lisp dilect instead of doing it in elisp, or polluting the emacs environment with your variables, or mistakenly messing something up. You can start a slime process with ~slime~ and use that process to evaluate everything. 177 | 178 | You Only need to set this configuration variable true: 179 | 180 | #+begin_src emacs-lisp :tangle yes 181 | (setq litex-use-slime-for-eval t) 182 | #+end_src 183 | 184 | ** Using LiTeX variables in your normal calculations 185 | You can use the function =litex-eval= to evaluate expressions for you. If you don't have slime integration then normal evaluation will work, but this will handle both cases for you. 186 | 187 | For example: 188 | #+begin_src emacs-lisp :tangle yes 189 | (defun calc-Q (a b c) 190 | (litex-eval `(+ (* C-1 ,b) (* C-2 ,a) (* C-3 ,c)))) 191 | #+end_src 192 | Here, the function calc-Q can be called from elisp, so you can put it in the org table formula, while internally it uses the variables =C-1= to =C-3= from your slime instance. 193 | 194 | ** Using Greek letters 195 | Someone who writes in LaTeX will definitely want to include greek letters, so you can use greek letters multiple ways in LiTeX. 196 | 197 | *** By double escaping the backslash 198 | You can use double escape to escape the backslash so you'll get the variable correct. For example: ~(setq \\alpha 2)~ ⇒ ~\alpha = 2~ 199 | 200 | *** Using Unicode: 201 | You can input unicode greek letters like α,β,γ...,Σ...,Ω, and they'll be rendered fine by LaTeX. For example: ~(setq α 2)~ ⇒ ~α = 2~. Which is the default behavior. 202 | 203 | If you want to use them to input, but still want to use LaTeX equivalent command then you can set ~litex-make-unicode-to-latex~ to true, that'll convert the unicode to LaTeX command. For example: ~(setq α 2)~ ⇒ ~{\alpha} = 2~. 204 | 205 | #+begin_src emacs-lisp :tangle yes 206 | (setq litex-make-unicode-to-latex t) 207 | #+end_src 208 | 209 | As for how to type unicode directly, you can use Compose key in Linux machines, and there is also TeX input method in emacs that lets you do that. If you type ~C-u C-\ TeX ~ for TeX input method then when you type ~\alpha~ emacs will convert it into unicode ~α~. 210 | 211 | *** Using conversion from their names 212 | By default you can use variables names like ~alpha~ without having it any effect, for example: ~(setq alpha 2)~ ⇒ ~alpha = 2~ but if you set the variable ~litex-make-name-to-latex-glyph~ true then you can just convert normal greek character's names to LaTeX symbols. 213 | Like: ~(setq alpha 2)~ ⇒ ~{\alpha} = 2~ 214 | 215 | #+begin_src emacs-lisp :tangle yes 216 | (setq litex-make-name-to-latex-glyph t) 217 | #+end_src 218 | 219 | When you have more than one letters, in this use case you have to separate them with ~.~, for example: 220 | ~(setq Delta.alpha 2)~ ⇒ ~{\Delta}{\alpha} = 2~ 221 | 222 | This is so that the letters inside other words won't be converted automatically. As you can see ~.~ is only used for initial separation of words so you can use it to make the bounds where you want greek letters: 223 | ~(setq Delta./.alpha 2)~ ⇒ ~{\Delta}/{\alpha} = 2~ 224 | 225 | * Explanation for functions 226 | 227 | ** litex-format-region-last 228 | Formats the selection based on variable ~litex-format-string~. 229 | 230 | For example: ~2.3434343~ ⇒ ~2.34~ (when ~litex-format-string~ is ~.2f~) 231 | 232 | ** litex-format-region 233 | Same as ~litex-format-region-last~ but asks for the format, it also sets the ~litex-format-string~ variable. 234 | 235 | *NOTE*: Doesn't work well with multiple-cursors, so first use this once, then use the ~litex-format-region-last~ on the multiple cursors. 236 | 237 | ** litex-eval-and-replace 238 | Evals the last sexp and replaces it with the evaluation value. 239 | 240 | ** litex-eval-and-insert 241 | Evals the last sexp and inserts the evaluation value after that. 242 | 243 | The value and sexp are separated by ~litex-steps-join-string~ which is "= " by default. 244 | 245 | ** litex-sexp-to-latex-exp 246 | Converts valid lisp sexp to latex Expression: 247 | 248 | For example: ~(+ 2 3 (* 6 x))~ ⇒ ~2 + 3 + 6 x~ 249 | 250 | ** litex-sexp-solve-all-steps 251 | Solves lisp sexp steps by steps: 252 | 253 | For example: 254 | ~(setq x 5)~ ⇒ ~x = 5~ then ~(setq y (+ 2 3 (* 6 x)))~ ⇒ ~y = (+ 2 3 (* 6 x)) = (+ 2 3 (* 6 5)) = (+ 2 3 30) = 35~ 255 | 256 | ** litex-increment-number 257 | Increments the number. 258 | 259 | ~some/url/to/chapter-2~ ⇒ ~some/url/to/chapter-3~ 260 | 261 | ** litex-exp-to-latex 262 | Converts exponential term to latex format. 263 | 264 | ~1.23e-34~ ⇒ ~1.23 \times 10^{-34}~ 265 | 266 | 267 | ** litex-exp-in-latex-math 268 | Encloses the selection in latex inline math. 269 | 270 | ~1.23e-34~ ⇒ ~\(1.23e-34\)~ 271 | 272 | ** litex-sexp-solve-all-steps-equation 273 | Same as ~litex-sexp-solve-all-steps~ but puts them in equation environment. 274 | 275 | For example: ~(setq y (+ 2 3 (* 6 x)))~ ⇒ 276 | 277 | #+begin_example 278 | \begin{equation} 279 | y= 2 + 3 + 6 x = 2 + 3 + 6 \times 5 = 2 + 3 + 30 = 35 280 | \end{equation} 281 | #+end_example 282 | 283 | ** litex-sexp-solve-all-steps-eqnarray 284 | Same as ~litex-sexp-solve-all-steps~ but puts them in eqnarray* environment. 285 | 286 | 287 | For example: ~(setq y (+ 2 3 (* 6 x)))~ ⇒ 288 | 289 | #+begin_example 290 | \begin{eqnarray*} 291 | y &=& 2 + 3 + 6 x \\ 292 | &=& 2 + 3 + 6 \times 5 \\ 293 | &=& 2 + 3 + 30\\ 294 | &=& 35 295 | \end{eqnarray*} 296 | #+end_example 297 | 298 | 299 | * Customization 300 | There are lots of variables that define how each of these functions behave. 301 | 302 | | Variable Name | Default Value | What it does | 303 | |---------------------------------------+------------------------+-----------------------------------------------------------------------------------------| 304 | | litex-latex-functions | '(sin cos tan) | Lisp functions that have their own latex commands. | 305 | | litex-make-hyphenated-to-subscript | t | Whether to make the hyphenated variables subscript or not. | 306 | | litex-latex-always-enclose? | nil | Enclose latex converted to paran all the time. | 307 | | litex-format-float-string | "%.3f" | Format string to be used by floats. | 308 | | litex-format-float-upper-limit | 1e4 | Upper limit of what number is formatted as float. | 309 | | litex-format-float-lower-limit | 1e-2 | Lower limit of what number is formatted as float. | 310 | | litex-format-float-trim-decimal | nil | Trim zeros after decimal if all decimals are zeros. | 311 | | litex-steps-join-string | "= " | String used for joining strings in steps of a solution. | 312 | | litex-steps-end-string | " " | String used at the end of each strings in steps of a solution. | 313 | | litex-math-inline-start | "\\(" | Opening syntax for math inline environment. | 314 | | litex-math-inline-end | "\\)" | Closing syntax for math inline environment. | 315 | | litex-math-equation-start | "\\begin{equation}\n" | Opening syntax for math equation environment. | 316 | | litex-math-equation-end | "\n\\end{equation}\n" | Closing syntax for math equation environment. | 317 | | litex-math-steps-equation-join-string | "= " | Value of `litex-steps-join-string' to be used in equation environment. | 318 | | litex-math-steps-equation-end-string | " " | Value of `litex-steps-end-string' to be used in equation environment. | 319 | | litex-math-eqnarray-start | "\\begin{eqnarray*}\n" | Opening syntax for math eqnarray environment. | 320 | | litex-math-eqnarray-end | "\n\\end{eqnarray*}\n" | Closing syntax for math eqnarray environment. | 321 | | litex-math-steps-eqnarray-join-string | " &=& " | Value of `litex-steps-join-string' to be used in eqnarray environment. | 322 | | litex-math-steps-eqnarray-end-string | "\\\\\n" | Value of `litex-steps-end-string' to be used in eqnarray environment. | 323 | | litex-math-align-start | "\\begin{align*}\n" | Opening syntax for math align environment. | 324 | | litex-math-align-end | "\n\\end{align*}\n" | Closing syntax for math align environment. | 325 | | litex-math-steps-align-join-string | "& = " | Value of `litex-steps-join-string' to be used in align environment. | 326 | | litex-math-steps-align-end-string | "\\\\\n" | Value of `litex-steps-end-string' to be used in align environment. | 327 | | litex-make-unicode-to-latex | nil | Convert unicode to LaTeX equivalent (eg. α -> \alpha) | 328 | | litex-make-name-to-latex-glyph | nil | Convert variables with the same name as a glyph to a LaTeX glyph (eg. alpha -> \alpha). | 329 | | litex-use-slime-for-eval | nil | Whether to use slime process for evalulation or not. You need to start slime yourself. | 330 | | litex-greek-unicode-latex-alist | | Alist of greek unicode symbols and their LaTeX counterparts. | 331 | 332 | 333 | * Contributing 334 | Since this package is new, I'd appreciate contributions on few things: 335 | 336 | - Finding bugs and reporting them in github issues. 337 | - There are many tests to be written for the functions. 338 | - Many functions that might have special syntax in LaTeX yet to be written. For example ~1+~, ~defun~ were added later (it only started with 4 operators), similar could be done for many more. 339 | - Fixing some glitches with the current functions. 340 | - Maybe some symbolic calculations using ~calc-eval~ if it has variables that are not yet defined. 341 | - I'm thinking of making an org babel implementation where it'll generate the equations that we can include in latex export. 342 | -------------------------------------------------------------------------------- /images/litex.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atreyagaurav/litex-mode/ada0a841829759147fc102316459959b92702559/images/litex.png -------------------------------------------------------------------------------- /litex-mode.el: -------------------------------------------------------------------------------- 1 | ;;; litex-mode.el --- Minor mode for converting lisp to LaTeX -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright (C) 2022 4 | 5 | ;; Author: Gaurav Atreya 6 | ;; URL: https://github.com/Atreyagaurav/litex-mode 7 | ;; Version: 0.1 8 | ;; Keywords: calculator, lisp, LaTeX 9 | ;; Package-Requires: ((emacs "24.4") (units-mode "0.1.1")) 10 | 11 | ;; This program is free software; you can redistribute it and/or 12 | ;; modify it under the terms of the GNU General Public License as 13 | ;; published by the Free Software Foundation, either version 3 of the 14 | ;; License, or (at your option) any later version. 15 | 16 | ;; This program is distributed in the hope that it will be useful, 17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | ;; GNU General Public License for more details. 20 | 21 | ;; You should have received a copy of the GNU General Public License 22 | ;; along with this program. If not, see 23 | ;; . 24 | 25 | ;;; Commentary: 26 | 27 | ;; converts valid mathematical Lisp expressions to LaTeX snippets 28 | ;; improved from https://emacs.stackexchange.com/a/70360 modified with 29 | ;; help from 30 | ;; https://gist.github.com/bpanthi977/4b8ece0eeff3bc05bb82275a23cbb56d 31 | 32 | ;; For detailed help visit github page: 33 | ;; https://github.com/Atreyagaurav/litex-mode 34 | 35 | ;;; Code: 36 | (eval-when-compile (require 'pcase)) 37 | (eval-when-compile (require 'subr-x)) 38 | (require 'cl-lib) 39 | (require 'ob-lisp) 40 | (require 'units-mode) 41 | 42 | ;; list from: 43 | ;; https://www.overleaf.com/learn/latex/Operators#Reference_guide 44 | ;; not all of these functions might have lisp equivalent, check it. 45 | (defvar litex-latex-functions 46 | '(cos csc exp ker limsup min sinh arcsin cosh deg gcd lg ln Pr sup 47 | arctan cot det hom lim log sec tan arg coth dim liminf max 48 | sin tanh) 49 | "Lisp functions that have their own latex commands.") 50 | (defvar litex-make-unicode-to-latex nil 51 | "Whether to convert unicode to LaTeX equivalent (eg. α -> \alpha). 52 | These work better in math mode.") 53 | (defvar litex-make-name-to-latex-glyph nil 54 | "Convert variables with the same name as a glyph to a LaTeX glyph. 55 | (eg. alpha -> \alpha).") 56 | (defvar litex-make-hyphenated-to-subscript t 57 | "Whether to make the hyphenated variables subscript or not.") 58 | (defvar litex-latex-always-enclose? nil 59 | "Always enclose latex converted with paran.") 60 | (defvar litex-keep-sexp-in-buffer nil 61 | "Keep the last sexp on point if true, else replace it.") 62 | 63 | (defvar litex-format-float-string "%.3f" 64 | "Format string to be used by floats.") 65 | (defvar litex-format-float-upper-limit 1e4 66 | "Upper limit of what number is formatted as float.") 67 | (defvar litex-format-float-lower-limit 1e-2 68 | "Lower limit of what number is formatted as float.") 69 | (defvar litex-format-float-trim-decimal nil 70 | "Trim zeros after decimal if all decimals are zeros.") 71 | 72 | (defvar litex-steps-join-string "= " 73 | "String used for joining strings in steps of a solution.") 74 | (defvar litex-steps-end-string " " 75 | "String used at the end of each strings in steps of a solution.") 76 | 77 | (defvar litex-math-inline-start "\\(" 78 | "Opening syntax for math inline environment.") 79 | (defvar litex-math-inline-end "\\)" 80 | "Closing syntax for math inline environment.") 81 | (defvar litex-math-brackets-start "\\left(" 82 | "Opening syntax for math brackets.") 83 | (defvar litex-math-brackets-end "\\right)" 84 | "Closing syntax for math brackets.") 85 | 86 | (defvar litex-math-equation-start "\\begin{equation}\n" 87 | "Opening syntax for math equation environment.") 88 | (defvar litex-math-equation-end "\n\\end{equation}\n" 89 | "Closing syntax for math equation environment.") 90 | (defvar litex-math-steps-equation-join-string "= " 91 | "Value of `litex-steps-join-string' used in equation environment.") 92 | (defvar litex-math-steps-equation-end-string " " 93 | "Value of `litex-steps-end-string' used in equation environment.") 94 | 95 | (defvar litex-math-eqnarray-start "\\begin{eqnarray*}\n" 96 | "Opening syntax for math eqnarray environment.") 97 | (defvar litex-math-eqnarray-end "\n\\end{eqnarray*}\n" 98 | "Closing syntax for math eqnarray environment.") 99 | (defvar litex-math-steps-eqnarray-join-string " &=& " 100 | "Value of `litex-steps-join-string' used in eqnarray environment.") 101 | (defvar litex-math-steps-eqnarray-end-string "\\\\\n" 102 | "Value of `litex-steps-end-string' used in eqnarray environment.") 103 | 104 | (defvar litex-math-align-start "\\begin{align*}\n" 105 | "Opening syntax for math align environment.") 106 | (defvar litex-math-align-end "\n\\end{align*}\n" 107 | "Closing syntax for math align environment.") 108 | (defvar litex-math-steps-align-join-string "& = " 109 | "Value of `litex-steps-join-string' used in align environment.") 110 | (defvar litex-math-steps-align-end-string "\\\\\n" 111 | "Value of `litex-steps-end-string' used in align environment.") 112 | 113 | (defvar litex-use-slime-for-eval nil 114 | "Whether to use slime process for evalulation or not. 115 | You need to start slime yourself.") 116 | 117 | 118 | (defvar litex-greek-unicode-latex-alist 119 | '(("α" . "alpha") 120 | ("β" . "beta") 121 | ("γ" . "gamma") 122 | ("δ" . "delta") 123 | ("ε" . "epsilon") 124 | ("ϵ" . "varepsilon") 125 | ("ζ" . "zeta") 126 | ("η" . "eta") 127 | ("θ" . "theta") 128 | ("ϑ" . "vartheta") 129 | ("ι" . "iota") 130 | ("κ" . "kappa") 131 | ("λ" . "lambda") 132 | ("μ" . "mu") 133 | ("ν" . "nu") 134 | ("ξ" . "xi") 135 | ("π" . "pi") 136 | ("ρ" . "rho") 137 | ("ϱ" . "varrho") 138 | ("σ" . "sigma") 139 | ("τ" . "tau") 140 | ("υ" . "upsilon") 141 | ("φ" . "phi") 142 | ("φ" . "varphi") 143 | ("χ" . "chi") 144 | ("ψ" . "psi") 145 | ("ω" . "omega") 146 | 147 | ("Γ" . "Gamma") 148 | ("Δ" . "Delta") 149 | ("Ζ" . "Zeta") 150 | ("Θ" . "Theta") 151 | ("Λ" . "Lambda") 152 | ("Ξ" . "Xi") 153 | ("Π" . "Pi") 154 | ("Ρ" . "Rho") 155 | ("Σ" . "Sigma") 156 | ("Υ" . "Upsilon") 157 | ("Φ" . "Phi") 158 | ("Ψ" . "Psi") 159 | ("Ω" . "Omega")) 160 | "Alist of greek unicode symbols and their LaTeX counterparts.") 161 | 162 | 163 | (defun litex-varible-is-ratio (var) 164 | "Check if the slime output of ratio type was mistaken as a symbol. 165 | Argument VAR: variable which could be from slime output." 166 | (if (string-match-p "[0-9]+/[0-9]+" (prin1-to-string var)) t nil)) 167 | 168 | (defun litex-process-slime-output (output) 169 | "Format the OUTPUT from slime to litex form." 170 | (pcase (type-of output) 171 | ('symbol (let ((out-str (prin1-to-string output))) 172 | (if (litex-varible-is-ratio output) 173 | (let ((parts (split-string out-str "/")) 174 | (litex-format-float-trim-decimal t)) 175 | (/ (* 1.0 (read (car parts))) (read (cadr parts)))) 176 | (if (string-match "\\([0-9]+\\)\\\\?\\([0-9.]+\\)d\\([+-]?[0-9]+\\)" out-str) 177 | (read (format "%s%se%s" 178 | (match-string 1 out-str) 179 | (match-string 2 out-str) 180 | (match-string 3 out-str))) 181 | output)))) 182 | (_ output))) 183 | 184 | (defun litex-eval (expr) 185 | "Eval funcion used by LiTeX, evaluate the EXPR in elisp or slime." 186 | (if litex-use-slime-for-eval 187 | (litex-process-slime-output 188 | (org-babel-execute:lisp (prin1-to-string expr) '())) 189 | (eval expr))) 190 | 191 | ;; Formatting functions 192 | (defun litex-format-float-not-within-limits (val) 193 | "Check is VAL is within float limits." 194 | (let ((sign (if (< val 0) -1 1))) 195 | (or (< (* sign val) litex-format-float-lower-limit) 196 | (> (* sign val) litex-format-float-upper-limit)))) 197 | 198 | 199 | (defun litex-format-float (val &optional nolim) 200 | "Function that defines how float VAL is formatted in lisp2latex." 201 | (let ((sign (if (< val 0) -1 1))) 202 | (if (and (not nolim) (litex-format-float-not-within-limits val)) 203 | (let* ((exponent (floor (log (* sign val) 10))) 204 | (front (/ val (expt 10 exponent)))) 205 | (concat (litex-format-float front t) 206 | " \\times 10^{" 207 | (number-to-string exponent) "}")) 208 | (let ((formatted (format litex-format-float-string val))) 209 | (if litex-format-float-trim-decimal 210 | (string-trim-right formatted "[.]0+") 211 | formatted))))) 212 | 213 | 214 | (defun litex-read-sexp-maybe-kill () 215 | "Read the sexp before point. 216 | Kill it if `litex-keep-sexp-in-buffer' is nil." 217 | (interactive) 218 | (let ((expr (sexp-at-point))) 219 | (if (not litex-keep-sexp-in-buffer) 220 | (backward-kill-sexp) 221 | ;; should I do it here or check if it's a comment and only do 222 | ;; that later. or copy the things after the sexp till EOL. 223 | (or (end-of-line) (insert "\n"))) 224 | expr)) 225 | 226 | 227 | (defun litex-format-greek-characters (string) 228 | "Format STRING to Greek LaTeX notation. 229 | Replace greek unicode or character name to latex notation." 230 | (let ((var-str string) 231 | (var-assoc nil)) 232 | (when litex-make-name-to-latex-glyph 233 | (when (rassoc var-str litex-greek-unicode-latex-alist) 234 | (setq var-str (concat "{\\" var-str "}")))) 235 | (when litex-make-unicode-to-latex 236 | (setq var-assoc (cdr (assoc var-str litex-greek-unicode-latex-alist))) 237 | (when var-assoc 238 | (setq var-str 239 | (concat "{\\" var-assoc "}")))) 240 | var-str)) 241 | 242 | 243 | (defun litex-format-variable (var) 244 | "Format variable VAR for LaTeX." 245 | (let ((var-strs (reverse 246 | (mapcar 247 | (lambda (s) (mapconcat #'litex-format-greek-characters 248 | (split-string s "*") "")) 249 | (split-string (prin1-to-string var t) "-"))))) 250 | 251 | (let ((var-final (car var-strs))) 252 | (if (> (length var-strs) 1) 253 | (cl-loop for (cvar . rest) on (cl-rest var-strs) do 254 | (setq var-final (format "%s_{%s}" cvar var-final)))) 255 | var-final))) 256 | 257 | 258 | ;; this function needs some serious thoughts 259 | (defun litex-latex-maybe-enclose (form &optional parent-func) 260 | "Encloses FORM which is argument of PARENT-FUNC in parantheis. 261 | If `litex-latex-always-enclose?' is true or if it determines it's necessary." 262 | (let* ((latex (litex-lisp2latex-all form))) 263 | (if (or litex-latex-always-enclose? 264 | (and 265 | (consp form) 266 | (litex-latex-enclose-check-args (cdr form)) 267 | (and parent-func 268 | (litex-latex-enclose-check-function parent-func)))) 269 | (format " %s %s %s " 270 | litex-math-brackets-start 271 | latex 272 | litex-math-brackets-end) 273 | latex))) 274 | 275 | 276 | (defun litex-latex-enclose-check-args (args) 277 | "Check if we need to use parantheis based on ARGS." 278 | (and (listp args) 279 | (or (> (length args) 1) 280 | (listp (car args))))) 281 | 282 | 283 | (defun litex-latex-enclose-check-function (func) 284 | "Check if we need to use parantheis for args based on FUNC. 285 | 286 | Return true if that function may need its argument to be in brackets 287 | if they are multiple arguments." 288 | (if (member func '(+ 1+ 1- expt / defun setq)) 289 | nil 290 | (if (member func '(- *)) 291 | t 292 | nil))) 293 | 294 | 295 | ;; formatting functions to be called by `litex-lisp2latex-all', each 296 | ;; one corresponds to the function at the end with args as arguments. 297 | (defun litex-format-args-+ (args) 298 | "Formatting function for + operator called with ARGS." 299 | (mapconcat #'litex-latex-maybe-enclose args " + ")) 300 | 301 | 302 | (defun litex-format-args-- (args) 303 | "Formatting function for - operator called with ARGS." 304 | (let ((arg1 (car args)) 305 | (arg-rest (cdr args))) 306 | (if arg-rest 307 | (format "%s - %s" (litex-latex-maybe-enclose arg1) 308 | (mapconcat (lambda (a) 309 | (litex-latex-maybe-enclose a '-)) 310 | arg-rest " - ")) 311 | (format "-%s" (litex-latex-maybe-enclose arg1 '-))))) 312 | 313 | 314 | (defun litex-format-args-* (args) 315 | "Formatting function for * operator called with ARGS." 316 | (with-output-to-string 317 | (cl-loop for (me next . rest) on args do 318 | (princ (format "%s" 319 | (litex-latex-maybe-enclose me '*))) 320 | (if (and next 321 | (or (and (symbolp me) 322 | (> (length (prin1-to-string me)) 1)) 323 | (and (symbolp next) 324 | (> (length (prin1-to-string next)) 1)) 325 | (numberp next))) 326 | (princ " \\times "))))) 327 | 328 | (defun litex-format-args-/ (args) 329 | "Formatting function for / operator called with ARGS." 330 | (let ((arg1 (car args)) 331 | (arg-rest (cdr args))) 332 | (if arg-rest 333 | (format "\\frac{%s}{%s}" 334 | (litex-latex-maybe-enclose arg1 '/) 335 | (litex-latex-maybe-enclose (cons '* arg-rest) '/)) 336 | (format "\\frac1{%s}" (litex-latex-maybe-enclose arg1 '/))))) 337 | 338 | 339 | (defun litex-format-args-1+ (args) 340 | "Formatting function for 1+ called with ARGS operator." 341 | (concat (litex-latex-maybe-enclose (car args) '1+) " + 1")) 342 | 343 | 344 | (defun litex-format-args-1- (args) 345 | "Formatting function for 1- called with ARGS operator." 346 | (concat (litex-latex-maybe-enclose (car args) '1-) " - 1")) 347 | 348 | 349 | (defun litex-format-args-expt (args) 350 | "Formatting function for expt function called with ARGS." 351 | (let ((base (car args)) 352 | (power (cadr args))) 353 | (if (or (listp base) 354 | ;; for cases where the base will be using × 10^{} format. 355 | (and (numberp base) 356 | (litex-format-float-not-within-limits base))) 357 | (format "%s %s %s^{%s}" 358 | litex-math-brackets-start 359 | (litex-latex-maybe-enclose base 'expt) 360 | litex-math-brackets-end 361 | (litex-latex-maybe-enclose power 'expt)) 362 | (format "%s^{%s}" 363 | (litex-latex-maybe-enclose base 'expt) 364 | (litex-latex-maybe-enclose power 'expt))))) 365 | 366 | 367 | (defun litex-format-args-sqrt (args) 368 | "Formatting function for sqrt function called with ARGS." 369 | (format "\\sqrt{%s}" (litex-latex-maybe-enclose (car args) 'sqrt))) 370 | 371 | 372 | (defun litex-format-args-setq (args) 373 | "Formatting function for setq function called with ARGS." 374 | (with-output-to-string 375 | (cl-loop for (a b . rest) on args by #'cddr do 376 | (princ (format "%s = %s" 377 | (litex-latex-maybe-enclose a 'setq) 378 | (litex-latex-maybe-enclose b 'setq))) 379 | (when rest (princ "; "))))) 380 | 381 | 382 | (setf (symbol-function 'litex-format-args-local-setq) 383 | #'litex-format-args-setq) 384 | 385 | 386 | (defun litex-format-args-list (args) 387 | (with-output-to-string 388 | (cl-loop for arg in args 389 | do (princ (format "%s = %s; " 390 | (litex-lisp2latex-all arg) 391 | (litex-lisp2latex-all 392 | (litex-eval arg))))))) 393 | 394 | (defun litex-format-args-defun (args) 395 | "Formatting function for defun called with ARGS function." 396 | (let ((func-name (car args)) 397 | (fargs (cadr args)) 398 | (expr (caddr args))) 399 | (format "\\text{%s}(%s) = %s" 400 | (litex-format-variable func-name) 401 | (mapconcat #'prin1-to-string fargs ",") 402 | (litex-latex-maybe-enclose expr 'defun)))) 403 | 404 | (defun litex-format-args-units-convert-simple (args) 405 | (let ((expr (car args)) 406 | (from-unit (cadr args))) 407 | (if (litex-is-final-form expr) 408 | (format "\\unit[%s]{%s}" 409 | (litex-latex-maybe-enclose expr 'units-convert-simple) 410 | from-unit) 411 | (litex-latex-maybe-enclose expr)))) 412 | 413 | (defun litex-format-args-units-ignore (args) 414 | (let ((expr (car args)) 415 | (unit (cadr args))) 416 | (if (litex-is-final-form expr) 417 | (format "\\unit[%s]{%s}" 418 | (litex-latex-maybe-enclose expr 'units-ignore) 419 | unit) 420 | (litex-latex-maybe-enclose expr)))) 421 | 422 | 423 | (defun litex-format-args-default (func args) 424 | "Default Formatting function for Lisp expressions. 425 | 426 | Call corresponding called with ARGS formatting function if 427 | available for FUNC passing ARGS as argument, else make a general 428 | format." 429 | (let ((func-symbol (intern (format "litex-format-args-%s" func)))) 430 | (if (functionp func-symbol) 431 | (apply func-symbol (list args)) 432 | (let* ((known? (cl-find func litex-latex-functions)) 433 | (enclose? (or (not known?) 434 | (litex-latex-enclose-check-args args))) 435 | (format-string 436 | (concat (if known? "\\%s" "\\text{%s}") 437 | (if enclose? 438 | (concat litex-math-brackets-start 439 | "%s" 440 | litex-math-brackets-end) 441 | " %s")))) 442 | (format format-string func 443 | (mapconcat #'litex-latex-maybe-enclose args ",")))))) 444 | 445 | 446 | (defun litex-lisp2latex-all (form) 447 | "Convert given Lisp expression FORM to latex equivalent string." 448 | (pcase form 449 | 450 | ;; functions 451 | (`(,func . ,args) (litex-format-args-default func args)) 452 | 453 | ;; simple variables 454 | (_ 455 | (cond ((floatp form) (litex-format-float form)) 456 | ((or (symbolp form) (stringp form)) 457 | (litex-format-variable form)) 458 | (t (prin1-to-string form)))))) 459 | 460 | 461 | (defun litex-contains-variables (expression) 462 | "Check if given EXPRESSION has variables." 463 | (if (functionp expression) 464 | nil 465 | (if (symbolp expression) 466 | t 467 | (if (consp expression) 468 | (cl-some #'litex-contains-variables expression) 469 | nil)))) 470 | 471 | 472 | (defun litex-substitute-values (expression) 473 | "Gives a string from EXPRESSION substituting the values." 474 | (condition-case nil 475 | (if (functionp expression) 476 | (format "%s" expression) 477 | (if (symbolp expression) 478 | (format "%s" (litex-eval expression)) 479 | (if (consp expression) 480 | (format "(%s)" 481 | (mapconcat #'litex-substitute-values 482 | expression " ")) 483 | (prin1-to-string expression)))) 484 | ;; this will catch error for undefined variables. 485 | (void-variable (prin1-to-string expression)))) 486 | 487 | 488 | (defun litex-units-single-step (form) 489 | (pcase form 490 | (`(units-convert ,_ ,unit) (list 'units-ignore (litex-eval form) unit)) 491 | (`(units-convert-simple ,_ ,_ ,unit) 492 | (list 'units-ignore 493 | (litex-eval form) unit)) 494 | (`(units-reduce ,_) (litex-eval form)) 495 | (`(units-ignore ,exp ,_) (litex-solve-single-step exp)) 496 | (_ (error "Unknown units function.")))) 497 | 498 | (defun litex-units-is-final-form (form) 499 | (pcase form 500 | (`(units-convert ,_ ,_) nil) 501 | (`(units-convert-simple ,_ ,_ ,_) nil) 502 | (`(units-reduce ,_) nil) 503 | (`(units-ignore ,exp ,_) (if (litex-is-final-form exp) t nil)) 504 | (_ nil))) 505 | 506 | 507 | (defun litex-is-final-form (form) 508 | (or (numberp form) 509 | (and (symbolp form) (litex-varible-is-ratio form)) 510 | (stringp form) 511 | (and (listp form) (string= (car form) 'list)))) 512 | 513 | 514 | (defun litex-solve-single-step (form) 515 | "Solves a single step of calculation in FORM." 516 | (cond ((listp form) 517 | (if (cl-every #'litex-is-final-form (cl-rest form)) 518 | (if (string-match-p "^units-" 519 | (symbol-name (car form))) 520 | (if (litex-units-is-final-form form) 521 | (litex-eval form) 522 | (litex-units-single-step form)) 523 | (litex-eval form)) 524 | (cons (car form) 525 | (mapcar #'litex-solve-single-step (cl-rest form))))) 526 | ((functionp form) 527 | form) 528 | 529 | ((symbolp form) 530 | (symbol-value form)) 531 | 532 | (t form))) 533 | 534 | 535 | (defun litex-solve-all-steps (form) 536 | "Solves all the steps of calculations in FORM expression. 537 | Retuns a list of steps." 538 | (if (litex-is-final-form form) 539 | (list form) 540 | (let 541 | ((solution (list form))) ;given expression 542 | 543 | (if 544 | (litex-contains-variables form) 545 | (setq solution 546 | (append solution 547 | (list 548 | (setq form 549 | (read 550 | (litex-substitute-values form))))))) 551 | 552 | (while (and (consp form) 553 | (not (litex-units-is-final-form form))) 554 | (setq solution 555 | (append solution 556 | (list (setq form 557 | (litex-solve-single-step form)))))) 558 | solution))) 559 | 560 | 561 | (defun litex-sexp-to-solved-string (expression format-func &optional setq-join) 562 | "Return solution of EXPRESSION using FORMAT-FUNC to format steps. 563 | Optionally use SETQ-JOIN for setq function variable and value 564 | defaults to `litex-steps-join-string'." 565 | (pcase expression 566 | (`(setq ,var ,exp) 567 | (concat 568 | (format "%s%s" 569 | (litex-format-variable var) 570 | (or setq-join litex-steps-join-string)) 571 | (mapconcat format-func 572 | (litex-solve-all-steps exp) 573 | (concat litex-steps-end-string 574 | litex-steps-join-string)))) 575 | (_ (mapconcat format-func 576 | (litex-solve-all-steps expression) 577 | (concat litex-steps-end-string 578 | litex-steps-join-string))))) 579 | 580 | 581 | (defun litex-eval-and-replace () 582 | "Replace the preceding sexp with its value." 583 | (interactive) 584 | (backward-kill-sexp) 585 | (condition-case nil 586 | (prin1 (litex-eval (read (current-kill 0))) 587 | (current-buffer)) 588 | (error (message "Invalid expression") 589 | (insert (current-kill 0))))) 590 | 591 | 592 | (defun litex-eval-and-insert () 593 | "Insert the evaulation result of the preceding sexp." 594 | (interactive) 595 | (backward-kill-sexp) 596 | (condition-case nil 597 | (insert (current-kill 0) 598 | litex-steps-join-string 599 | (format "%s" 600 | (litex-eval (read (current-kill 0))))) 601 | (error (message "Invalid expression")))) 602 | 603 | 604 | (defun litex-exp-to-latex (beg end) 605 | "Convert exponentials in the given region to latex notation. 606 | Argument BEG begining position of region. 607 | Argument END end position of region." 608 | (interactive "r") 609 | (save-excursion 610 | (goto-char end) 611 | (while (re-search-backward 612 | "\\([0-9.+-]+\\)e\\([0-9.+-]+\\)" beg) 613 | (replace-match "\\1 \\\\times 10^{\\2}")))) 614 | 615 | (defun litex-exp-in-latex-math (beg end) 616 | "Insert the selected expression inside latex inline math environment. 617 | Argument BEG begining position of region. 618 | Argument END end position of region." 619 | (interactive "r") 620 | (save-excursion 621 | (goto-char beg) 622 | (insert litex-math-inline-start) 623 | (goto-char (+ (point) (- end beg))) 624 | (insert litex-math-inline-end))) 625 | 626 | 627 | (defun litex-sexp-to-latex-exp () 628 | "Convert valid sexp to latex expressions." 629 | (interactive) 630 | (insert (litex-lisp2latex-all (litex-read-sexp-maybe-kill)))) 631 | 632 | 633 | (defun litex-sexp-replace-variables () 634 | "Replace the variable values in the last sexp at point." 635 | (interactive) 636 | (insert (litex-substitute-values (litex-read-sexp-maybe-kill)))) 637 | 638 | 639 | (defun litex-sexp-solve-all-steps () 640 | "Solve last sexp at point in steps and insert those steps." 641 | (interactive) 642 | (let ((expression (litex-read-sexp-maybe-kill))) 643 | (insert 644 | (litex-sexp-to-solved-string expression #'prin1-to-string)))) 645 | 646 | 647 | (defun litex-sexp-solve-single-step () 648 | "Solve last sexp at point for one step and insert it." 649 | (interactive) 650 | (let ((expression (litex-read-sexp-maybe-kill))) 651 | (insert 652 | (prin1-to-string (litex-solve-single-step expression))))) 653 | 654 | 655 | (defun litex-solve-all-steps-equation () 656 | "Solve last sexp in steps and insert it in LaTeX equation environment." 657 | (interactive) 658 | (let ((expression (litex-read-sexp-maybe-kill)) 659 | (litex-steps-join-string litex-math-steps-equation-join-string) 660 | (litex-steps-end-string litex-math-steps-equation-end-string)) 661 | (insert litex-math-equation-start 662 | (litex-sexp-to-solved-string expression #'litex-lisp2latex-all) 663 | litex-math-equation-end))) 664 | 665 | 666 | (defun litex-solve-all-steps-eqnarray () 667 | "Solve last sexp in steps and insert it in LaTeX eqnarray environment." 668 | (interactive) 669 | (let ((expression (litex-read-sexp-maybe-kill)) 670 | (litex-steps-join-string litex-math-steps-eqnarray-join-string) 671 | (litex-steps-end-string litex-math-steps-eqnarray-end-string)) 672 | (insert litex-math-eqnarray-start 673 | (litex-sexp-to-solved-string expression #'litex-lisp2latex-all) 674 | litex-math-eqnarray-end))) 675 | 676 | (defun litex-solve-all-steps-align () 677 | "Solve last sexp in steps and insert it in LaTeX align environment." 678 | (interactive) 679 | (let ((expression (litex-read-sexp-maybe-kill)) 680 | (litex-steps-join-string litex-math-steps-align-join-string) 681 | (litex-steps-end-string litex-math-steps-align-end-string)) 682 | (insert litex-math-align-start 683 | (litex-sexp-to-solved-string expression #'litex-lisp2latex-all) 684 | litex-math-align-end))) 685 | 686 | (defun litex-solve-region-all-steps-align (beg end) 687 | "Solve last sexp in steps and insert it in LaTeX eqnarray environment." 688 | (interactive "r") 689 | (let ((litex-steps-join-string litex-math-steps-align-join-string) 690 | (litex-steps-end-string litex-math-steps-align-end-string) 691 | (expressions (cdr (read 692 | (concat "(prog " 693 | (buffer-substring-no-properties beg end) 694 | ")"))))) 695 | (delete-region beg end) 696 | (insert litex-math-align-start 697 | (string-join 698 | (let ((litex-steps-join-string litex-math-steps-equation-join-string) 699 | (litex-steps-end-string litex-math-steps-equation-end-string)) 700 | (cl-loop for exp in expressions 701 | collect (litex-sexp-to-solved-string 702 | exp #'litex-lisp2latex-all litex-math-steps-align-join-string) 703 | do (litex-eval exp))) 704 | litex-steps-end-string) 705 | litex-math-align-end))) 706 | 707 | 708 | 709 | (defun litex-format-region-last (beg end) 710 | "Format region like last `litex-format-region` call (without query). 711 | BEG and END are region bounds." 712 | (interactive (if (use-region-p) 713 | (list (region-beginning) (region-end)) 714 | (let ((bnd (bounds-of-thing-at-point 'sexp))) 715 | (list (cl-first bnd) (cl-rest bnd))))) 716 | (let ((text (buffer-substring-no-properties beg end))) 717 | ;; maybe I should make it litex-eval if given expression 718 | (if (string-match-p "%[0-9.]*[dfex]" litex-format-float-string) 719 | (setq text (litex-eval (read text)))) 720 | (kill-region beg end) 721 | (insert (format litex-format-float-string text)))) 722 | 723 | 724 | (defun litex-format-region (beg end) 725 | "Format selected region as per input format, BEG and END are region bounds." 726 | (interactive (if (use-region-p) 727 | (list (region-beginning) (region-end)) 728 | (let ((bnd (bounds-of-thing-at-point 'symbol))) 729 | (list (cl-first bnd) (cl-rest bnd))))) 730 | (let ((fmt (read-string "Enter format string: " 731 | litex-format-float-string))) 732 | (setq litex-format-float-string fmt) 733 | (litex-format-region-last beg end))) 734 | 735 | 736 | (defun litex-increment-number (step) 737 | "Increase the number value by STEP." 738 | (interactive "P") 739 | (let* ((bounds (if (use-region-p) 740 | (cons (region-beginning) (region-end)) 741 | (bounds-of-thing-at-point 'symbol))) 742 | (word (buffer-substring-no-properties 743 | (car bounds) 744 | (cdr bounds))) 745 | (step (or step 1))) 746 | (when bounds 747 | (let ((ma (string-match 748 | "\\(.*[^0-9]\\)\\([0-9]+\\)\\([/_.,-\"']\\)?" 749 | word))) 750 | (when ma 751 | (delete-region (car bounds) (cdr bounds)) 752 | (insert 753 | (match-string 1 word) 754 | (number-to-string (+ step (string-to-number 755 | (match-string 2 word)))) 756 | (match-string 3 word))))))) 757 | 758 | 759 | 760 | (defun litex-insert-or-replace-x (beg end) 761 | "If a region (BEG to END) is selected, replace * by \times 762 | otherwise insert \times instead of ×." 763 | (interactive (if (use-region-p) 764 | (list (region-beginning) (region-end)) 765 | (list (point) (point)))) 766 | (goto-char end) 767 | (if (= beg end) 768 | (insert "\\times") 769 | (when (re-search-backward "*" beg) 770 | (replace-match "\\\\times")))) 771 | 772 | 773 | ;; I'm not making a litex-mode-map because I don't want it to come by 774 | ;; default, user can choose to apply this keymap to some other key as 775 | ;; prefix key, like C-e in readme, or just map individual functions. 776 | (defvar litex-key-map 777 | (let ((keymap (make-sparse-keymap))) 778 | (define-key keymap (kbd "F") 'litex-format-region) 779 | (define-key keymap (kbd "f") 'litex-format-region-last) 780 | (define-key keymap (kbd "E") 'litex-eval-and-replace) 781 | (define-key keymap (kbd "e") 'litex-eval-and-insert) 782 | (define-key keymap (kbd "s") 'litex-sexp-to-latex-exp) 783 | (define-key keymap (kbd "S") 'litex-sexp-solve-all-steps) 784 | (define-key keymap (kbd "r") 'litex-sexp-replace-variables) 785 | (define-key keymap (kbd "+") 'litex-increment-number) 786 | (define-key keymap (kbd "l") 'litex-exp-to-latex) 787 | (define-key keymap (kbd "m") 'litex-exp-in-latex-math) 788 | (define-key keymap (kbd "A") 'litex-solve-all-steps-equation) 789 | (define-key keymap (kbd "a") 'litex-solve-all-steps-align) 790 | (define-key keymap (kbd "C-a") 'litex-solve-all-steps-eqnarray) 791 | (define-key keymap (kbd "C-r") 'litex-solve-region-all-steps-align) 792 | keymap)) 793 | 794 | (define-minor-mode litex-mode 795 | "Minor mode for Calculations on lisp, and formatting on LaTeX." 796 | :lighter " LiTeX") 797 | 798 | 799 | (provide 'litex-mode) 800 | 801 | ;;; litex-mode.el ends here 802 | -------------------------------------------------------------------------------- /litex-mode.texi: -------------------------------------------------------------------------------- 1 | \input texinfo @c -*- texinfo -*- 2 | @c %**start of header 3 | @setfilename litex-mode.info 4 | @settitle 5 | @documentencoding UTF-8 6 | @documentlanguage en 7 | @c %**end of header 8 | 9 | @finalout 10 | @titlepage 11 | @title 12 | @end titlepage 13 | 14 | @contents 15 | 16 | @ifnottex 17 | @node Top 18 | @top 19 | @end ifnottex 20 | 21 | @menu 22 | * Li@TeX{} mode:: 23 | * How to Use:: 24 | * Installation:: 25 | * Usage:: 26 | * Keybindings:: 27 | * Known Problems:: Known Problems. 28 | * Tips and Tricks:: 29 | * Explanation for functions:: 30 | * Customization:: 31 | * Contributing:: 32 | 33 | @detailmenu 34 | --- The Detailed Node Listing --- 35 | 36 | Keybindings 37 | 38 | * Setting keybindings for individual functions:: 39 | * Setting the whole litex provided keybindings to a prefix key:: 40 | * Complete setup using use-package:: 41 | 42 | Tips and Tricks 43 | 44 | * Using case sensitive symbols:: 45 | * Slime integration:: 46 | * Using Greek letters:: 47 | 48 | Using Greek letters 49 | 50 | * By double escaping the backslash:: 51 | * Using Unicode:: Using Unicode. 52 | * Using conversion from their names:: 53 | 54 | Explanation for functions 55 | 56 | * litex-format-region-last:: 57 | * litex-format-region:: 58 | * litex-eval-and-replace:: 59 | * litex-eval-and-insert:: 60 | * litex-sexp-to-latex-exp:: 61 | * litex-sexp-solve-all-steps:: 62 | * litex-increment-number:: 63 | * litex-exp-to-latex:: 64 | * litex-exp-in-latex-math:: 65 | * litex-sexp-solve-all-steps-equation:: 66 | * litex-sexp-solve-all-steps-eqnarray:: 67 | 68 | @end detailmenu 69 | @end menu 70 | 71 | @node Li@TeX{} mode 72 | @chapter Li@TeX{} mode 73 | 74 | This is a minor mode to convert valid elisp/lisp expressions to latex. 75 | 76 | This is useful for emacs users because emacs allows lisp code to be directly evaluated inside any buffer, which means no need to have any code blocks. But since lisp expressions aren't popular we can't expect to put them in reports, so to overcome that I wrote this mode with the help of my friend @uref{https://gist.github.com/bpanthi977, Bibek Panthi}. 77 | 78 | The beginning of this mode, and previous codes are here: 79 | @itemize 80 | @item 81 | @uref{https://emacs.stackexchange.com/questions/70358/how-can-i-convert-a-lisp-expression-to-a-latex-math-expression/70360?noredirect=1#comment113158_70360, Stackexchange Question} 82 | @item 83 | @uref{https://gist.github.com/bpanthi977/4b8ece0eeff3bc05bb82275a23cbb56d, Bibek's gist} 84 | @item 85 | @uref{https://github.com/Atreyagaurav/emacs-modules, My previous calculation template mode and first few versions of Li@TeX{}} 86 | @end itemize 87 | 88 | Not only can it convert lisp expressions to latex, it can also, give intermediate solution steps. Perfect for doing homeworks (as that's what I made it for) :P 89 | 90 | @node How to Use 91 | @chapter How to Use 92 | 93 | Here is a @uref{https://youtu.be/esEf63gSSPg, demo video} describing the features of Li@TeX{} mode. 94 | 95 | For Text descriptions refer sections below for details on what each function does, and the meaning of custom variables. 96 | 97 | You can also access it all from texinfo file provided with the package. 98 | 99 | @node Installation 100 | @chapter Installation 101 | 102 | You can install it through melpa with @code{M-x package-install litex-mode }. 103 | 104 | Or you can clone this github repo and load it using path. 105 | 106 | Configuration section shows how you can add the load path using @code{use-package}. 107 | 108 | @node Usage 109 | @chapter Usage 110 | 111 | Using @code{use-package} you can do: 112 | 113 | @lisp 114 | (use-package litex-mode 115 | ;; :load-path "/path/to/litex-mode/" 116 | :commands litex-mode 117 | :hook text-mode) 118 | @end lisp 119 | 120 | Please look through the Keybindings, Functions, Variables, Known problems and the Tips and Tricks to get 100% out of this package. 121 | 122 | @node Keybindings 123 | @chapter Keybindings 124 | 125 | By default Li@TeX{}-mode doesn't provide any keybindings, but it does have a variable containing bindings for the interactive functions that you can use. 126 | 127 | @menu 128 | * Setting keybindings for individual functions:: 129 | * Setting the whole litex provided keybindings to a prefix key:: 130 | * Complete setup using use-package:: 131 | @end menu 132 | 133 | @node Setting keybindings for individual functions 134 | @section Setting keybindings for individual functions 135 | 136 | You can use @code{local-set-key} to bind individual functions to a key binding. 137 | @lisp 138 | (local-set-key (kbd "×") 'litex-insert-or-replace-x) 139 | @end lisp 140 | 141 | @node Setting the whole litex provided keybindings to a prefix key 142 | @section Setting the whole litex provided keybindings to a prefix key 143 | 144 | You can set a prefix key (@code{C-e} for me here) like in this example, which makes it so you can for example use @code{litex-format-region-last} using @code{C-e f} using the following in your config. In some cases you might have to unset key @code{C-e} because it's used to goto end of line, I'm replacing that because I don't use it (as @code{End} key does the same). 145 | @lisp 146 | (local-set-key (kbd "C-e") litex-key-map) 147 | @end lisp 148 | 149 | 150 | Contents of litex-key-map are below. 151 | 152 | @lisp 153 | (define-key litex-key-map (kbd "F") 'litex-format-region) 154 | (define-key litex-key-map (kbd "f") 'litex-format-region-last) 155 | (define-key litex-key-map (kbd "E") 'litex-eval-and-replace) 156 | (define-key litex-key-map (kbd "e") 'litex-eval-and-insert) 157 | (define-key litex-key-map (kbd "s") 'litex-sexp-to-latex-exp) 158 | (define-key litex-key-map (kbd "S") 'litex-sexp-solve-all-steps) 159 | (define-key litex-key-map (kbd "+") 'litex-increment-number) 160 | (define-key litex-key-map (kbd "l") 'litex-exp-to-latex) 161 | (define-key litex-key-map (kbd "m") 'litex-exp-in-latex-math) 162 | (define-key litex-key-map (kbd "A") 'litex-sexp-solve-all-steps-equation) 163 | (define-key litex-key-map (kbd "a") 'litex-sexp-solve-all-steps-eqnarray) 164 | @end lisp 165 | 166 | @node Complete setup using use-package 167 | @section Complete setup using use-package 168 | 169 | This is the complete setup using use-package, if you installed from melpa. If you installed by cloning the repo, uncomment and provide the load path. 170 | @lisp 171 | (use-package litex-mode 172 | ;; :load-path "/path/to/litex-mode/" 173 | :commands litex-mode 174 | :hook text-mode 175 | :config 176 | (local-set-key (kbd "C-e") litex-key-map) 177 | (local-set-key (kbd "×") 'litex-insert-or-replace-x)) 178 | @end lisp 179 | 180 | @node Known Problems 181 | @chapter Known Problems: 182 | 183 | elisp uses integer calculations so @code{(/ 1 2)} is evaluated to 0, be careful of such pitfalls. For now @code{(/ 1.0 2)} is evaluated as @code{0.5}, so I'd recommend using floats when you need floats. 184 | 185 | This problem doesn't exist if you use slime integration. 186 | 187 | @node Tips and Tricks 188 | @chapter Tips and Tricks 189 | 190 | @menu 191 | * Using case sensitive symbols:: 192 | * Slime integration:: 193 | * Using Greek letters:: 194 | @end menu 195 | 196 | @node Using case sensitive symbols 197 | @section Using case sensitive symbols 198 | 199 | Inside emacs the symbols are read without case sensitivity, so if you define and variable names @code{ABCD}, it'll replace the variable named @code{abcd}. To avoid that, specially if you have formula with both lowercase and uppercase symbols you can use this customization. 200 | 201 | @lisp 202 | (setq readtable-case :preserve) 203 | @end lisp 204 | 205 | NOTE: Currently it only works for elisp, and not for slime integration, I'm searching for a solution with slime. 206 | 207 | @node Slime integration 208 | @section Slime integration 209 | 210 | If you want to do the calculations in your favorite lisp dilect instead of doing it in elisp, or polluting the emacs environment with your variables, or mistakenly messing something up. You can start a slime process with @code{slime} and use that process to evaluate everything. 211 | 212 | You Only need to set this configuration variable true: 213 | 214 | @lisp 215 | (setq litex-use-slime-for-eval t) 216 | @end lisp 217 | 218 | @node Using Greek letters 219 | @section Using Greek letters 220 | 221 | Someone who writes in @LaTeX{} will definitely want to include greek letters, so you can use greek letters multiple ways in Li@TeX{}. 222 | 223 | @menu 224 | * By double escaping the backslash:: 225 | * Using Unicode:: Using Unicode. 226 | * Using conversion from their names:: 227 | @end menu 228 | 229 | @node By double escaping the backslash 230 | @subsection By double escaping the backslash 231 | 232 | You can use double escape to escape the backslash so you'll get the variable correct. For example: @code{(setq \\alpha 2)} ⇒ @code{\alpha = 2} 233 | 234 | @node Using Unicode 235 | @subsection Using Unicode: 236 | 237 | You can input unicode greek letters like α,β,γ@dots{},Σ@dots{},Ω, and they'll be rendered fine by @LaTeX{}. For example: @code{(setq α 2)} ⇒ @code{α = 2}. Which is the default behavior. 238 | 239 | If you want to use them to input, but still want to use @LaTeX{} equivalent command then you can set @code{litex-make-unicode-to-latex} to true, that'll convert the unicode to @LaTeX{} command. For example: @code{(setq α 2)} ⇒ @code{@{\alpha@} = 2}. 240 | 241 | @lisp 242 | (setq litex-make-unicode-to-latex t) 243 | @end lisp 244 | 245 | As for how to type unicode directly, you can use Compose key in Linux machines, and there is also @TeX{} input method in emacs that lets you do that. If you type @code{C-u C-\ TeX } for @TeX{} input method then when you type @code{\alpha} emacs will convert it into unicode @code{α}. 246 | 247 | @node Using conversion from their names 248 | @subsection Using conversion from their names 249 | 250 | By default you can use variables names like @code{alpha} without having it any effect, for example: @code{(setq alpha 2)} ⇒ @code{alpha = 2} but if you set the variable @code{litex-make-name-to-latex-glyph} true then you can just convert normal greek character's names to @LaTeX{} symbols. 251 | Like: @code{(setq alpha 2)} ⇒ @code{@{\alpha@} = 2} 252 | 253 | @lisp 254 | (setq litex-make-name-to-latex-glyph t) 255 | @end lisp 256 | 257 | @node Explanation for functions 258 | @chapter Explanation for functions 259 | 260 | @menu 261 | * litex-format-region-last:: 262 | * litex-format-region:: 263 | * litex-eval-and-replace:: 264 | * litex-eval-and-insert:: 265 | * litex-sexp-to-latex-exp:: 266 | * litex-sexp-solve-all-steps:: 267 | * litex-increment-number:: 268 | * litex-exp-to-latex:: 269 | * litex-exp-in-latex-math:: 270 | * litex-sexp-solve-all-steps-equation:: 271 | * litex-sexp-solve-all-steps-eqnarray:: 272 | @end menu 273 | 274 | @node litex-format-region-last 275 | @section litex-format-region-last 276 | 277 | Formats the selection based on variable @code{litex-format-string}. 278 | 279 | For example: @code{2.3434343} ⇒ @code{2.34} (when @code{litex-format-string} is @code{.2f}) 280 | 281 | @node litex-format-region 282 | @section litex-format-region 283 | 284 | Same as @code{litex-format-region-last} but asks for the format, it also sets the @code{litex-format-string} variable. 285 | 286 | @strong{NOTE}: Doesn't work well with multiple-cursors, so first use this once, then use the @code{litex-format-region-last} on the multiple cursors. 287 | 288 | @node litex-eval-and-replace 289 | @section litex-eval-and-replace 290 | 291 | Evals the last sexp and replaces it with the evaluation value. 292 | 293 | @node litex-eval-and-insert 294 | @section litex-eval-and-insert 295 | 296 | Evals the last sexp and inserts the evaluation value after that. 297 | 298 | The value and sexp are separated by @code{litex-steps-join-string} which is "= " by default. 299 | 300 | @node litex-sexp-to-latex-exp 301 | @section litex-sexp-to-latex-exp 302 | 303 | Converts valid lisp sexp to latex Expression: 304 | 305 | For example: @code{(+ 2 3 (* 6 x))} ⇒ @code{2 + 3 + 6 x} 306 | 307 | @node litex-sexp-solve-all-steps 308 | @section litex-sexp-solve-all-steps 309 | 310 | Solves lisp sexp steps by steps: 311 | 312 | For example: 313 | @code{(setq x 5)} ⇒ @code{x = 5} then @code{(setq y (+ 2 3 (* 6 x)))} ⇒ @code{y = (+ 2 3 (* 6 x)) = (+ 2 3 (* 6 5)) = (+ 2 3 30) = 35} 314 | 315 | @node litex-increment-number 316 | @section litex-increment-number 317 | 318 | Increments the number. 319 | 320 | @code{some/url/to/chapter-2} ⇒ @code{some/url/to/chapter-3} 321 | 322 | @node litex-exp-to-latex 323 | @section litex-exp-to-latex 324 | 325 | Converts exponential term to latex format. 326 | 327 | @code{1.23e-34} ⇒ @code{1.23 \times 10^@{-34@}} 328 | 329 | @node litex-exp-in-latex-math 330 | @section litex-exp-in-latex-math 331 | 332 | Encloses the selection in latex inline math. 333 | 334 | @code{1.23e-34} ⇒ @code{\(1.23e-34\)} 335 | 336 | @node litex-sexp-solve-all-steps-equation 337 | @section litex-sexp-solve-all-steps-equation 338 | 339 | Same as @code{litex-sexp-solve-all-steps} but puts them in equation environment. 340 | 341 | For example: @code{(setq y (+ 2 3 (* 6 x)))} ⇒ 342 | 343 | @example 344 | \begin@{equation@} 345 | y= 2 + 3 + 6 x = 2 + 3 + 6 \times 5 = 2 + 3 + 30 = 35 346 | \end@{equation@} 347 | @end example 348 | 349 | @node litex-sexp-solve-all-steps-eqnarray 350 | @section litex-sexp-solve-all-steps-eqnarray 351 | 352 | Same as @code{litex-sexp-solve-all-steps} but puts them in eqnarray* environment. 353 | 354 | 355 | For example: @code{(setq y (+ 2 3 (* 6 x)))} ⇒ 356 | 357 | @example 358 | \begin@{eqnarray*@} 359 | y &=& 2 + 3 + 6 x \\ 360 | &=& 2 + 3 + 6 \times 5 \\ 361 | &=& 2 + 3 + 30\\ 362 | &=& 35 363 | \end@{eqnarray*@} 364 | @end example 365 | 366 | @node Customization 367 | @chapter Customization 368 | 369 | There are lots of variables that define how each of these functions behave. 370 | 371 | @multitable {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa} {aaaaaaaaaaaaaaaaaaaaaa} {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa} 372 | @headitem Variable Name 373 | @tab Default Value 374 | @tab What it does 375 | @item litex-latex-functions 376 | @tab '(sin cos tan) 377 | @tab Lisp functions that have their own latex commands. 378 | @item litex-make-hyphenated-to-subscript 379 | @tab t 380 | @tab Whether to make the hyphenated variables subscript or not. 381 | @item litex-latex-maybe-enclose? 382 | @tab nil 383 | @tab Enclose latex converted to paran if needed. 384 | @item litex-format-float-string 385 | @tab "%.3f" 386 | @tab Format string to be used by floats. 387 | @item litex-format-float-upper-limit 388 | @tab 1e4 389 | @tab Upper limit of what number is formatted as float. 390 | @item litex-format-float-lower-limit 391 | @tab 1e-2 392 | @tab Lower limit of what number is formatted as float. 393 | @item litex-steps-join-string 394 | @tab "= " 395 | @tab String used for joining strings in steps of a solution. 396 | @item litex-steps-end-string 397 | @tab " " 398 | @tab String used at the end of each strings in steps of a solution. 399 | @item litex-math-inline-start 400 | @tab "\\(" 401 | @tab Opening syntax for math inline environment. 402 | @item litex-math-inline-end 403 | @tab "\\)" 404 | @tab Closing syntax for math inline environment. 405 | @item litex-math-equation-start 406 | @tab "\" 407 | @tab Opening syntax for math equation environment. 408 | @item litex-math-equation-end 409 | @tab "\" 410 | @tab Closing syntax for math equation environment. 411 | @item litex-math-steps-equation-join-string 412 | @tab "= " 413 | @tab Value of `litex-steps-join-string' to be used in equation environment. 414 | @item litex-math-steps-equation-end-string 415 | @tab " " 416 | @tab Value of `litex-steps-end-string' to be used in equation environment. 417 | @item litex-math-eqnarray-start 418 | @tab "\" 419 | @tab Opening syntax for math eqnarray environment. 420 | @item litex-math-eqnarray-end 421 | @tab "\" 422 | @tab Closing syntax for math eqnarray environment. 423 | @item litex-math-steps-eqnarray-join-string 424 | @tab " &=& " 425 | @tab Value of `litex-steps-join-string' to be used in eqnarray environment. 426 | @item litex-math-steps-eqnarray-end-string 427 | @tab "\\\\" 428 | @tab Value of `litex-steps-end-string' to be used in eqnarray environment. 429 | @item litex-math-align-start 430 | @tab "\" 431 | @tab Opening syntax for math align environment. 432 | @item litex-math-align-end 433 | @tab "\" 434 | @tab Closing syntax for math align environment. 435 | @item litex-math-steps-align-join-string 436 | @tab "& = " 437 | @tab Value of `litex-steps-join-string' to be used in align environment. 438 | @item litex-math-steps-align-end-string 439 | @tab "\\\\" 440 | @tab Value of `litex-steps-end-string' to be used in align environment. 441 | @item litex-make-unicode-to-latex 442 | @tab nil 443 | @tab Convert unicode to @LaTeX{} equivalent (eg. α -> α) 444 | @item litex-make-name-to-latex-glyph 445 | @tab nil 446 | @tab Convert variables with the same name as a glyph to a @LaTeX{} glyph (eg. alpha -> α). 447 | @item litex-use-slime-for-eval 448 | @tab nil 449 | @tab Whether to use slime process for evalulation or not. You need to start slime yourself. 450 | @item litex-greek-unicode-latex-alist 451 | @tab 452 | @tab Alist of greek unicode symbols and their @LaTeX{} counterparts. 453 | @end multitable 454 | 455 | @node Contributing 456 | @chapter Contributing 457 | 458 | Since this package is new, I'd appreciate contributions on few things: 459 | 460 | @itemize 461 | @item 462 | Finding bugs and reporting them in github issues. 463 | @item 464 | There are many tests to be written for the functions. 465 | @item 466 | Many functions that might have special syntax in @LaTeX{} yet to be written. For example @code{1+}, @code{defun} were added later (it only started with 4 operators), similar could be done for many more. 467 | @item 468 | Fixing some glitches with the current functions. 469 | @item 470 | Maybe some symbolic calculations using @code{calc-eval} if it has variables that are not yet defined. 471 | @end itemize 472 | 473 | @bye 474 | -------------------------------------------------------------------------------- /tests/litex-mode-tests.el: -------------------------------------------------------------------------------- 1 | ;; Run all tests using (ert t) 2 | 3 | ;; format tests 4 | (ert-deftest litex-process-slime-output-test () 5 | (let ((litex-format-float-string "%.3f") 6 | (litex-format-float-upper-limit 1e4) 7 | (litex-format-float-lower-limit 1e-2) 8 | (litex-format-float-trim-decimal nil)) 9 | (should (= (litex-process-slime-output '1/2) 0.5)) 10 | (should (= (litex-process-slime-output '1/20000) 11 | (/ 1 20000.0))) 12 | (should (= (litex-process-slime-output '1) 1)) 13 | (should (= (litex-process-slime-output '1.0001) 1.0001)))) 14 | 15 | (ert-deftest litex-format-float-test () 16 | (let ((litex-format-float-string "%.3f") 17 | (litex-format-float-upper-limit 1e4) 18 | (litex-format-float-lower-limit 1e-2)) 19 | (should (string= (litex-format-float 2.0) "2.000")) 20 | (let ((litex-format-float-trim-decimal t)) 21 | (should (string= (litex-format-float 2.0) "2")) 22 | (should (string= (litex-format-float 2.010) "2.010"))) 23 | (should (string= (litex-format-float 2.1234) "2.123")) 24 | (should (string= (litex-format-float 2.1236) "2.124"))) 25 | (let ((litex-format-float-string "%.2f")) 26 | (should (string= (litex-format-float 2.0) "2.00"))) 27 | (let ((litex-format-float-string "%d")) 28 | (should (string= (litex-format-float 2.0) "2"))) 29 | (let ((litex-format-float-string "%.2e")) 30 | (should (string= (litex-format-float 2.0) "2.00e+00")))) 31 | 32 | 33 | (ert-deftest litex-format-float-test-2 () 34 | (let ((litex-format-float-string "%.3f") 35 | (litex-format-float-upper-limit 1e4) 36 | (litex-format-float-lower-limit 1e-2)) 37 | (should (string= (litex-format-float 2.0e5) 38 | "2.000 \\times 10^{5}")) 39 | (should (string= (litex-format-float 2.1234e-3) 40 | "2.123 \\times 10^{-3}")) 41 | (should (string= (litex-format-float 2.1236e-6) 42 | "2.124 \\times 10^{-6}")))) 43 | 44 | (ert-deftest litex-format-variable-test () 45 | (let ((litex-make-unicode-to-latex t) 46 | (litex-make-name-to-latex-glyph t)) 47 | (should (string= (litex-format-variable 'α) "{\\alpha}")) 48 | (should (string= (litex-format-variable 'α-β) "{\\alpha}_{{\\beta}}")) 49 | (should (string= (litex-format-variable 'α-β-t) "{\\alpha}_{{\\beta}_{t}}")) 50 | (should (string= (litex-format-variable 'i-j-k) "i_{j_{k}}")) 51 | (should (string= (litex-format-variable 'α-Δ*t-2) 52 | "{\\alpha}_{{\\Delta}t_{2}}")) 53 | (should (string= (litex-format-variable '\\alpha) "\\alpha")) 54 | (should (string= (litex-format-variable '\\alpha-\\beta) "\\alpha_{\\beta}")) 55 | (should (string= (litex-format-variable '\\alpha-\\Delta\ t-2) 56 | "\\alpha_{\\Delta t_{2}}")) 57 | (should (string= (litex-format-variable 'alpha) "{\\alpha}")) 58 | (should (string= (litex-format-variable 'Delta*alpha) 59 | "{\\Delta}{\\alpha}")) 60 | (should (string= (litex-format-variable 'alpha*beta) 61 | "{\\alpha}{\\beta}"))) 62 | ) 63 | 64 | 65 | ;; individual operator tests 66 | (ert-deftest litex-format-args-+-test () 67 | (should (string= (litex-format-args-+ '(1 2)) "1 + 2")) 68 | (should (string= (litex-format-args-+ '(x 2)) "x + 2")) 69 | (should (string= (litex-format-args-+ '(1 y)) "1 + y")) 70 | (should (string= (litex-format-args-+ '(x y)) "x + y"))) 71 | 72 | 73 | (ert-deftest litex-format-args---test () 74 | (should (string= (litex-format-args-- '(1 2)) "1 - 2")) 75 | (should (string= (litex-format-args-- '(x 2)) "x - 2")) 76 | (should (string= (litex-format-args-- '(1 y)) "1 - y")) 77 | (should (string= (litex-format-args-- '(x y)) "x - y")) 78 | (should (string= (litex-format-args-- '(3)) "-3")) 79 | (should (string= (litex-format-args-- '(x)) "-x"))) 80 | 81 | 82 | (ert-deftest litex-format-args-*-test () 83 | (should (string= (litex-format-args-* '(1 2)) "1 \\times 2")) 84 | (should (string= (litex-format-args-* '(x 2)) "x \\times 2")) 85 | (should (string= (litex-format-args-* '(2 y)) "2y")) 86 | (should (string= (litex-format-args-* '(x y)) "xy")) 87 | (should (string= (litex-format-args-* '(2 yz)) "2 \\times yz")) 88 | (should (string= (litex-format-args-* '(xy y)) "xy \\times y"))) 89 | 90 | 91 | (ert-deftest litex-format-args-/-test () 92 | (should (string= (litex-format-args-/ '(1 2)) "\\frac{1}{2}")) 93 | (should (string= (litex-format-args-/ '(x 2)) "\\frac{x}{2}")) 94 | (should (string= (litex-format-args-/ '(1 y)) "\\frac{1}{y}")) 95 | (should (string= (litex-format-args-/ '(1 2 y)) 96 | "\\frac{1}{2y}")) 97 | (should (string= (litex-format-args-/ '(1 2 y 3)) 98 | "\\frac{1}{2y \\times 3}")) 99 | (should (string= (litex-format-args-/ '(x y)) "\\frac{x}{y}")) 100 | (should (string= (litex-format-args-/ '(2)) "\\frac1{2}")) 101 | (should (string= (litex-format-args-/ '(x)) "\\frac1{x}"))) 102 | 103 | 104 | (ert-deftest litex-format-args-1+-test () 105 | (should (string= (litex-format-args-1+ '(1)) "1 + 1")) 106 | (should (string= (litex-format-args-1+ '(x)) "x + 1"))) 107 | 108 | 109 | (ert-deftest litex-format-args-expt-test () 110 | (should (string= (litex-format-args-expt '(1 2)) "1^{2}")) 111 | (should (string= (litex-format-args-expt '(x 2)) "x^{2}")) 112 | (should (string= (litex-format-args-expt 113 | '((+ 1 2) 2)) "\\left( 1 + 2 \\right)^{2}")) 114 | (should (string= (litex-format-args-expt '((* 2 x) 2)) 115 | "\\left( 2x \\right)^{2}"))) 116 | 117 | 118 | (ert-deftest litex-lisp2latex-all-test () 119 | (should (string= (litex-lisp2latex-all 120 | '(setq x 2)) "x = 2")) 121 | (should (string= (litex-lisp2latex-all 122 | '(setq x (- y 2))) "x = y - 2")) 123 | (should (string= (litex-lisp2latex-all 124 | '(setq x (expt y 2))) "x = y^{2}")) 125 | (should (string= (litex-lisp2latex-all 126 | '(setq z (expt (1+ x) y))) 127 | "z = \\left( x + 1 \\right)^{y}")) 128 | (should (string= (litex-lisp2latex-all 129 | '(setq z (expt (* 2 x) y))) 130 | "z = \\left( 2x \\right)^{y}")) 131 | (should (string= (litex-lisp2latex-all 132 | '(setq z (expt (1+ x) (* 2 y)))) 133 | "z = \\left( x + 1 \\right)^{2y}")) 134 | (should (string= (litex-lisp2latex-all '(1- (* 2 x))) "2x - 1")) 135 | (should (string= (litex-lisp2latex-all 136 | '(setq x (- t 6 (* 5 60 (/ x))))) 137 | "x = t - 6 - \\left( 5 \\times 60\\frac1{x} \\right) ")) 138 | ;; this one is failing 139 | (should (string= (litex-lisp2latex-all 140 | '(setq x (- t 6 (* 5 60 (/ x 2))))) 141 | "x = t - 6 - \\left( 5 \\times 60 \\left( \\frac{x}{2} \\right) \\right) ")) 142 | (should (string= (litex-lisp2latex-all 143 | '(fun x 2 (/ 5 (+ 6 7) x))) 144 | "\\text{fun}\\left(x,2,\\frac{5}{ \\left( 6 + 7 \\right) x}\\right)"))) 145 | --------------------------------------------------------------------------------