4

Currently I am performing calculations using excel and manually formatting them for printing using an online LaTeX equation editor. The end result looks something like this: enter image description here

As you may imagine this requires a lot of double-hadling of the parameters and, if any of the variables change, everything must be retyped. Is there any way where I could achieve a similar output directly through LaTeX?

I would like to be able to define the variables, some of which are calculated from other variables, and display the formulas, with both the parameters and the values shown. I don't need to perform any complex mathematics. Typically +,-,*,/ and exponents is all that would be required though basic functions (sin, ln, log) may also be useful.

3

I started working on it before JPi had answered, so I will post my solution anyways.

LuaTeX is ideal for this. For example, here is solution using ConTeXt (It should be easy to translate it to LaTeX).

\startluacode
  userdata = userdata or {}
  userdata.variables = 
    {
      m = 10,
      Cp = 30,
      T1 = 15,
      T2 = 20,
    }

  variables = userdata.variables

  variables.DeltaT = variables.T2 - variables.T1
  variables.Q = variables.m*variables.Cp*variables.DeltaT

  local function showRow(a,b,c)
      context("\\NC %s \\NC %s \\NC %s \\NC \\NR", a, b, c)
  end

  local function singleRow(a)
      context("\\NC[nc=3, align=middle] %s \\NC \\NR", a)
  end

  local format = string.format

  userdata.ShowVariables = function()
    context.startTABLE{setups="table:variables"}
    showRow("Parameter Value", "Units", "Description")
    showRow("$m$", format("\\unit{%s KiloGram}", variables.m), "Mass")
    showRow("$Cp$", format("\\unit{%s KiloJoule/Celsius KiloGram}", variables.Cp), "Heat Capacity")
    showRow("$T_1$", format("\\unit{%s Celsius}", variables.T1), "Initial Temperature")
    showRow("$T_2$", format("\\unit{%s Celsius}", variables.T2), "Final Temperature")
    singleRow(format("$ΔT = T_2 - T_1 = %s - %s = %s~[\\unit{Celsius}]$", variables.T2, variables.T1, variables.DeltaT))
    showRow("$ΔT$", format("\\unit{%s Celsius}", variables.DeltaT), "Temperature Difference")
    singleRow(format("$Q = m × Cp × ΔT = %s × %s × %s = %s~[\\unit{KiloJoule}]$", variables.m, variables.Cp, variables.DeltaT, variables.Q))
    showRow("$Q$", format("\\unit{%s KiloJoule}", variables.Q), "Heat")
    context.stopTABLE()
  end
\stopluacode

\startsetups table:variables
  \setupTABLE[frame=off, offset=0.25em]
  \setupTABLE[column][3][width=12em] 
  % Width needed, otherwise column span doesn't work
\stopsetups

\define\ShowVariables{\ctxlua{userdata.ShowVariables()}}

\starttext
\ShowVariables
\stoptext

which gives

enter image description here

I used a separate namespace userdata to avoid conflict with existing variable names.

2

Easy with lualatex:

%% compile with lualatex
\documentclass{article}

\usepackage{luacode}

\begin{luacode*}
 function celsius(t)
  tex.sprint((t-32)*5/9)
  end
\end{luacode*}
\newcommand{\celsius}[1]{\luadirect{celsius(\luastring{#1})}}

\begin{document}

\begin{tabular}{rr}
 50 & \celsius{50}\\
 -40 & \celsius{-40}
 \end{tabular}

 \end{document}

enter image description here

0
  • Currently I am performing calculations using excel and manually formatting them for printing using an online LaTeX equation editor.

  • tranquillity, LaTeX itself is the system for graphical representation of the math expression if we talk about math part of LaTeX. The most of programs that perform the calculations by LaTeX it needs to be the LaTeX text typed with special rules. Each program has its own rules. Then such a program parses the text, recognizes variables, their values and make the calculations. As an algorithm it always like the sequence of the following operations:
  • Typset LaTeX text of the special type were is specially specified such data as variables, their values, output format and so on;
  • launch the program that perform the cslculation.
  • I wish to offer a different solution. It seems there is exactly what you are looking for. That is the program that build the LaTeX string of math expression that you type in plain text easily. It performs calculations with variables, saves result to file, analyzes errors connected not only with wrong syntax, but it help find and even avoid such errors as loss of relevance (accuracy), and the possibility of accidental results (infinity, not a number).
  • That program is AnEasyCalc. Type in any search engin (without spaces) and you will find a lot of file storage servers when it can be downloaded.

You can find it also at developer's site: http://steamandwater.od.ua/AnEasyCalc/index.html

  • This demo version is unlimited by the use time, but has a limmited functionality. The main restriction is the sting length that can not be more 32. If we abandon long names and indexes fairly complex things can be calculated.

Good luck)

  • 1
    I'm afraid I don't really understand what you talk about in roughly the first part of your answer. Is it even relevant to the question at hand? The second half of your answer is not that clear to me either, but I can see a connection to the question. Your question suffers from somewhat confusing mark-up. Maybe you can make it more to the point and concise. – moewe Oct 10 '15 at 12:05
  • From reading another answer of yours mentioning AnEasyCalc I can't help but feel that this does the opposite of what the OP asks for. – moewe Oct 10 '15 at 12:06
  • That is so easy as aneasycalc is easy to use. – Igor Mar 10 '16 at 12:58
  • Sorry but the ENTER pressing momentaly sends the message. That is why the previous comment has appeared.) Look at question please: TC says: "Currently I am performing calculations using excel and manually formatting them for printing using an online LaTeX equation editor." and it needs to " a lot of double-hadling of the parameters and, if any of the variables change, everything must be retyped. " Than TC asks: "Is there any way where I could achieve a similar output directly through LaTeX? " MY ANSWER IS: try aneasycalc program thus it makes all as you want! What I have done wrong? – Igor Mar 10 '16 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.