TeX - LaTeX Asked by msun5040 on March 24, 2021
I’m relatively new to LaTeX and I still don’t really know how to use the newcommand
feature at all (new to stackexchange as well). I’ve been trying to find out how to print something like this
I’ve tried various ways and so far the code with the least number of errors is this:
newcommand{xmatrix}[6]{
begin{vmatrix}
$i & j & k$
{u1} & {u2} & {u3}
{v1} & {v2} & {v3}
end{vmatrix}}
I’m pretty sure I’m just using the newcommand
part wrong but I want to be able to print the unit vectors in bold in the first row and the next 6 numbers are input. Thank you for any advice!
You want to define a macro with six arguments: those six arguments are represented by #1
, ..., #6
in the macro code. Since vmatrix
is always in math mode there is no need to use $
dollar signs within it. You could therefore do something like this:
documentclass{article}
usepackage{amsmath} % for vmatrix
newcommand{xmatrix}[6]{%
begin{vmatrix}
mathbf{i} & mathbf{j} & mathbf{k}
#1 & #2 & #3
#4 & #5 & #6
end{vmatrix}}
begin{document}
[
xmatrix{u1}{u2}{u3}{v1}{v2}{v3}
]
end{document}
However, I find a macro taking six arguments a little bit unpleasant and prone to errors. I propose an alternative definition with two arguments, each of them being a comma-separated list.
documentclass{article}
usepackage{amsmath}
makeatletter
newcommand*{xmatrix}[2]{{%
def@xmatrix##1,##2,##3,##4relax{##1 & ##2 & ##3}%
begin{vmatrix}
mathbf{i} & mathbf{j} & mathbf{k}
@xmatrix#1,,,relax
@xmatrix#2,,,relax
end{vmatrix}%
}}
makeatother
begin{document}
[
xmatrix{u1,u2,u3}{v1,v2,v3}
quad
xmatrix{u1}{,v2,v3} % just as example
]
end{document}
Correct answer by campa on March 24, 2021
You can do much better than defining a macro with six arguments. I propose just two arguments, representing the coefficients of the vectors as comma separated lists.
We can also define a variant that actually computes the cross product.
documentclass{article}
usepackage{amsmath,xparse}
ExplSyntaxOn
NewDocumentCommand{crossproduct}{s m m}
{% #1 = optional *
% #2 = first vector as comma list
% #3 = second vector as comma list
IfBooleanTF{#1}
{% *-variant, compute the product
msun_crossproduct_compute:nn { #2 } { #3 }
}
{% normal, typeset in determinant form
msun_crossproduct_symbolic:nn { #2 } { #3 }
}
}
fp_new:N l_msun_crossproduct_i_fp
fp_new:N l_msun_crossproduct_j_fp
fp_new:N l_msun_crossproduct_k_fp
cs_new_protected:Nn msun_crossproduct_compute:nn
{
fp_set:Nn l_msun_crossproduct_i_fp
{
clist_item:nn { #1 } { 2 } * clist_item:nn { #2 } { 3 }
-
clist_item:nn { #1 } { 3 } * clist_item:nn { #2 } { 2 }
}
fp_set:Nn l_msun_crossproduct_j_fp
{
clist_item:nn { #1 } { 3 } * clist_item:nn { #2 } { 1 }
-
clist_item:nn { #1 } { 1 } * clist_item:nn { #2 } { 3 }
}
fp_set:Nn l_msun_crossproduct_k_fp
{
clist_item:nn { #1 } { 1 } * clist_item:nn { #2 } { 2 }
-
clist_item:nn { #1 } { 2 } * clist_item:nn { #2 } { 1 }
}
fp_use:N l_msun_crossproduct_i_fp mathbf{i}
fp_compare:nF { l_msun_crossproduct_j_fp < c_zero_fp } { + }
fp_use:N l_msun_crossproduct_j_fp mathbf{j}
fp_compare:nF { l_msun_crossproduct_k_fp < c_zero_fp } { + }
fp_use:N l_msun_crossproduct_k_fp mathbf{k}
}
cs_new_protected:Nn msun_crossproduct_symbolic:nn
{
begin{vmatrix}
mathbf{i} & mathbf{j} & mathbf{k}
clist_item:nn { #1 } { 1 } & clist_item:nn { #1 } { 2 } & clist_item:nn { #1 } { 3 }
clist_item:nn { #2 } { 1 } & clist_item:nn { #2 } { 2 } & clist_item:nn { #2 } { 3 }
end{vmatrix}
}
ExplSyntaxOff
begin{document}
[
crossproduct{0,1,-2}{3,0,-4}=
crossproduct*{0,1,-2}{3,0,-4}
]
end{document}
Answered by egreg on March 24, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP