TeX - LaTeX Asked by Qohelet on December 5, 2020
I’m new to Latex and have a background in software development. I find the ability of auto-generating formulas and lists impressive, but struggle to format the result properly as the letters are slightly too big. Please excuse I’m not familiar with the differences of elements as I’m still very much working on trial and error.
How would I define custom font-sizing for these loop-generated equations?
This here is my document which does work:
documentclass{article}
usepackage{tikz}
usepackage[nomessages]{fp}
usepackage{etoolbox}
usepackage{calc}
usepackage{mathtools}
defsquare[#1]{#1^2}
gdefsquareresult{}%
foreach i in {0,...,6}{%
FPevalA{square[i]}%
FPevalA{round(A:0)}%
xapptosquareresult{ pmb{A} = i^2 = i cdot i }%
}%
begin{document}
begin{align*}
squareresult
end{align*}
end{document}
How I see Latex seems to have issues using begin{equation} squareresult end{equation}
with foreach-loops (in this case there is no newline after each formula). Using gather*
instead of align*
seems to be a working option too, but frankly I wouldn’t know which one is preferred -and why.
Some sources suggest using [ textstyle
(or other styles) – which I was not able to make work in this setup, as it appears to be only working on equation
.
In general: Are there preferred ways to properly do a foreach-loop with a linebreak after each line and applying a specific styleset to it?
Use noexpandbm
instead of pmb
(load the bm
package after mathtools
).
However, you can do much better.
documentclass{article}
usepackage{mathtools,bm}
%usepackage{xparse} % not needed with LaTeX 2020-10-01 or later
ExplSyntaxOn
NewDocumentCommand{showexpr}{mmmm}
{% #1 = evaluation, #2 = output format, #3 = start, #4 = end
qohelet_showexpr:nnnn { #1 } { #2 } { #3 } { #4 }
}
seq_new:N l__qohelet_lines_seq
cs_new_protected:Nn qohelet_showexpr:nnnn
{
% the function to evaluate the expression according to the given template
cs_set:Nn __qohelet_eval:n { fp_eval:n { #1 } }
% an alias for the second argument
cs_set_eq:NN eval __qohelet_eval:n
% the function to print the output according to the given template
cs_set:Nn __qohelet_output:n { #2 }
% clear the sequence where we store the lines
seq_clear:N l__qohelet_lines_seq
% loop in the given range
int_step_inline:nnn { #3 } { #4 }
{
% add the corresponding line for the current value ##1
seq_put_right:Nn l__qohelet_lines_seq { __qohelet_output:n { ##1 } }
}
% output the results
begin{align*}
seq_use:Nn l__qohelet_lines_seq { }
end{align*}
}
ExplSyntaxOff
begin{document}
showexpr
{#1^2}
{bm{#1^2}&=eval{#1}=#1cdot#1}
{0}{6}
showexpr
{#1^2-2*#1+3}
{#1^2-2cdot#1+3&=eval{#1}}
{1}{4}
showexpr
{round(sqrt(#1),4)}
{sqrt{#1}&=eval{#1}}
{1}{6}
end{document}
The first argument to showexpr
is the expression to evaluate, using a fairly natural syntax. The second argument is the template for the output, where eval{#1}
stands for the computed result. The third and fourth arguments are the starting and ending point of the range.
Note that in both the first and second argument #1
stands for the current value in the loop.
With seq_use:Nn
we ensure that a blank line is not added at the end, which happens with your code (even if fixed with noexpand
).
Answered by egreg on December 5, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP