# Writing a macro in LaTeX for getting a vector i,j,k representation by passing parameters


\end{document}


-
Perhaps a little modification so that \vecty{0}{0}{0} produces 0? –  Gonzalo Medina Sep 27 '12 at 7:22
Refering to the question it should do nothing for \vecty{0}{0}{0}, which my solution already does. However, it can easily be modified if you want a 0 –  Herbert Sep 27 '12 at 7:27
Do as you please; I only mentioned to handle that case as a suggestion based on the mathematical meaning. –  Gonzalo Medina Sep 27 '12 at 7:31
@ガベージコレクタ Yes you can, just use \hat{\imath} and \hat{\jmath}. –  tohecz Sep 27 '12 at 8:07
@ガベージコレクタ: I suppose that it is not too difficult for you to replace 0 by \vec{0} ... –  Herbert Sep 27 '12 at 9:16

Here's a solution using lua, it doesn't offer any advantages over Peter's solution above, so I'm simply posting it for comparisons sake (and because I wanted to get some practice with lua). Compile with lualatex.

\documentclass{article}
\usepackage{luacode}

%\begin{filecontents*}{vectyfunc.lua}
\begin{luacode*}
function vecty_make (x,y,z)
local basis = {"\\hat{\\mathrm{i}}","\\hat{j}","\\hat{k}"}
local coeffs = {x,y,z}
local str = ""
local num = 0
for i = 1,3 do
coeffs[i] = string.gsub(coeffs[i]," ","")
-- if not empty
if coeffs[i]~="" then
-- and if not zero
if tonumber(coeffs[i])~=0 then
num = num + 1
-- then put a + if missing a sign
if tonumber(coeffs[i]) == 1 then coeffs[i] = "" end
if tonumber(coeffs[i]) == -1 then coeffs[i] = "-" end
if string.find(coeffs[i],"^[+-]") == nil then
coeffs[i]="+"..coeffs[i]
end
-- and append to str
str = str..coeffs[i]..basis[i]
end
end
end

if num == 0 then
tex.sprint("\\vec{0}")
else
-- remove first sign if a +
if string.sub(str,1,1) == "+" then
str = string.sub(str,2)
end
tex.sprint(str)
end

end
\end{luacode*}
%\end{filecontents*}

%\directlua{dofile("vectyfunc.lua")}

\newcommand{\vecty}[3]{
\directlua{vecty_make("#1","#2","#3")}
}

\begin{document}
$\begin{array}{ll} \verb|\vecty{1}{2}{3}| & \vecty{1}{2}{3} \\ \verb|\vecty{0}{3}{-3}| & \vecty{0}{3}{-3} \\ \verb|\vecty{0}{0}{0}| & \vecty{0}{0}{0} \\ \verb|\vecty{0}{0}{3}| & \vecty{0}{0}{3} \\ \verb|\vecty{1}{2}{0}| & \vecty{1}{2}{0} \\ \verb|\vecty{0}{3}{3}| & \vecty{0}{3}{3} \\ \verb|\vecty{3}{0}{0}| & \vecty{3}{0}{0} \\ \verb|\vecty{-1}{1}{0}| & \vecty{-1}{1}{0} \\ \verb|\vecty{- 1}{ - 1}{+ 0}| & \vecty{- 1}{ - 1}{+ 0}\\ \verb|\vecty{-1}{-1.0}{+2}| & \vecty{-1}{-1.0}{+2}\\ \verb|\vecty{-0.0}{+0}{0}| & \vecty{-0.0}{+0}{0}\\ \verb|\vecty{-0}{+x}{y}| & \vecty{-0}{+x}{y}\\ \verb|\vecty{-x}{+7}{y}| & \vecty{-x}{+7}{y}\\ \end{array}$
\end{document}

-