40 votes

Reducing fractions automatically using LaTeX3

Here is a flat LaTeX2e implementation. \documentclass{article} \usepackage{amsmath} \newcount{\numerator} \newcount{\denominator} \newcount{\gcd} % compute \gcd and returns reduced \numerator and \...
  • 71.3k
37 votes
Accepted

Animate mass-spring-damper TikZ

Update: Wheel "riding" on the road and moving observer, as requested in OP and comments. (Click on the image to run the interactive SVG [3.9 MiB] in the browser.) The governing ODE system ...
  • 51.8k
33 votes

Reducing fractions automatically using LaTeX3

If you are not bound to expl3 (in which case you “just” need to implement the algorithm): \documentclass{scrartcl} \usepackage{xintgcd,xintfrac} \newcommand*\reducedfrac[2] {\begingroup \edef\...
  • 26.4k
31 votes

Reducing fractions automatically using LaTeX3

An option using Lua+LaTeX. Made small improvement. Made a Lua function to be called as a LaTeX command, with the numerator and denominator passed as arguments, instead of hardcoding the values in as ...
  • 18.5k
29 votes

Simplifying square roots

Here's a LuaLaTeX-based solution. The code sets up a LaTeX macro named \rsqrt, which invokes a Lua function named rsqrt. The latter implements the simplification algorithm you've proposed -- with the ...
  • 449k
28 votes
Accepted

Sum of finite series using LaTeX 3

Yes, you can, and pretty easily too. \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \NewDocumentCommand{\computesum}{mmm} {% pass control to an internal function \svend_compute_sum:nnn ...
  • 1.0m
27 votes
Accepted

Reducing fractions automatically using LaTeX3

An expl3 implementation: \nonstopmode \input expl3-generic \relax \ExplSyntaxOn % -*- expl3 -*- \cs_new:Nn \svend_gcd:nn { \int_compare:nNnTF {#2} = { 0 } {#1} { \svend_gcd:ff {#2} { \...
  • 26.9k
26 votes
Accepted

Drawing the three altitudes of a triangle with TikZ; incorrect orthocenter

This is, indeed, due to some inaccuracies in PGF, and can actually been seen in the manual in the section on coordinate calculations. More specifically it appears to be down to the the \...
  • 69.2k
24 votes

simple arithmetic on index of foreach statement in tikz

Two solutions using only foreach own computing tools \foreach \x [count=\i] in {3.14,6.28,...,21.98} Difference between second and first items in list is calculate and added to second and ...
  • 130k
21 votes

Sum of (example: 0+1+2+3 = 6 , 0+1+2+3+4+5+6+7 = 28 and so on)

The difficult task is generating the terms of the sequence, not computing the sum, of course; I present a macro that prints all the terms or just the sum. You can define a different starting point and ...
  • 1.0m
21 votes

Longest Collatz sequence calculated with LaTeX

Reimplementation of Joseph's answer in LuaTeX. The nice thing about doing computations in Lua is that they are always fully-expandable. \documentclass{article} \usepackage{luacode} \begin{luacode} ...
19 votes

How to get the sum, difference, product, and quotient from a macro in ConTeXt or Plain TeX?

You can use the ε-TeX primitive \numexpr for expandable integer expressions. The only restriction is that it won't work in engines without ε-TeX extensions (mostly Knuth TeX nowadays), ...
18 votes

Simplifying square roots

In expl3: \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \NewDocumentCommand{\rsqrt}{m} { \manual_rsqrt:n { #1 } } \int_new:N \l_manual_rsqrt_int \cs_new_protected:Nn \manual_rsqrt:...
  • 1.0m
18 votes

How to transcript codes from HP 50G in LaTeX

This answer tries to reproduce the screenshot. It uses the lcd package, which defines an extended alphanumeric character set, with each character constructed using dots set in a 5 x 7 matrix. Thus, ...
  • 5,451
17 votes
Accepted

Using LaTeX as calculator

Here's a solution that uses LuaLaTeX. No additional floating-point routines need be loaded. The output precision is controlled by the first argument of the string.format function; in the example below,...
  • 449k
17 votes

Longest Collatz sequence calculated with LaTeX

I would do this using an expandable implementation: it's easy enough to keep everything on the stack: \documentclass{article} \usepackage{expl3} \usepackage{xparse} \begin{document} \ExplSyntaxOn \...
  • 245k
17 votes
Accepted

How to make text appear in the middle of a phantom in math mode?

You don't really need to use \mathclap, you can just place what you want to center in a box of the appropriate width. \documentclass{article} \usepackage{amsmath} \usepackage{calc} \newcommand*{\mask}...
  • 16.3k
16 votes

Page Numbering Using Only Fibonacci Numbers

Package fibnum helps: \usepackage{fibnum} \renewcommand*{\thepage}{\fibnum{\value{page}}} Full example: \documentclass{article} \usepackage{fibnum} \renewcommand*{\thepage}{\fibnum{\value{page}}} \...
16 votes
Accepted

Total number of points

As long as you are just dealing with integer points, you can use a counter to sum them up. As you want to include the sum before they are actually added, the package totcount comes in handy: \...
15 votes
Accepted

How to compute the difference between two time points (e.g., 11:30 am and 01:20 pm => 110 min)?

\documentclass{article} \def\foo#1#2{% \the\numexpr(\xfoo#2\relax)-(\xfoo#1\relax)\relax\ minutes} \def\xfoo#1:#2 #3m#4\relax{% %(#1)*60+#2\if p#3+720 \fi (#1)*60+#2\if p#3\ifnum#1=12 \else+720\fi\...
15 votes

Length plus minus other length?

Two alternatives to David's answer: e-TeX provides \glueexpr, which allows to add glue specifications: \newlength{\textsize} \makeatletter \setlength{\textsize}{\f@size pt} \makeatother \setlength{\...
15 votes

Simplifying square roots

And here is an example how to do this using only classical TeX: \newcount\numA \newcount\numB \newcount\numC \newcount\numD \def\rsqrt#1{\numA=2 \numC=#1 \rsqrtA} \def\rsqrtA{% \numB=\numA \...
  • 56.1k
15 votes
Accepted

Sort Numbers After Their Values

Easy with xparse and expl3: \documentclass{article} \usepackage{xparse,expl3} \ExplSyntaxOn \NewDocumentCommand{\sortnumberlist}{m} { \clist_set:Nn \l_svend_numberlist_clist { #1 } \clist_sort:...
  • 1.0m
15 votes
Accepted

Tikz wrong calculation draw exponential function

This is caused by how the pgf math function exp is implemented. A simplified example: \documentclass{article} \usepackage{pgfmath, pgffor} \begin{document} \foreach \i in {8.9, 9.0, 9.1} { \...
  • 22.9k
14 votes
Accepted

Rounding a number to its hundred

Use siunitx and expl3. \documentclass{article} \usepackage{xparse,siunitx} \ExplSyntaxOn \NewDocumentCommand{\hundreds}{O{}m} { \num[#1]{\fp_eval:n { trunc(#2,-2) }} } \ExplSyntaxOff \begin{...
  • 1.0m
14 votes

Tikz: drawing in perspective

Another alternative from tikz-3dplot that provides more fun stuffs. Set the xyz coordinate system to xy coordinate system by \tdplotsetmaincoords{90}{90} Draw a large square first. Determine the ...
  • 29.1k
14 votes
Accepted

What is the best way to do math inside LaTeX macros?

Here's a LuaLaTeX-based method for setting up the macro \solvedegtan{24}: % !TEX TS-program = lualatex \documentclass{article} \newcommand\solvedegtan[1]{% \directlua{ tex.sprint ( math.tan ( ...
  • 449k

Only top scored, non community-wiki answers of a minimum length are eligible