How can I program a command \mycommand such, that a white space is set printed behind it if a white space is used after it and no white space is printed if no white space is used (e.g. when punctuation is used) without using {} after the command usage. I understand that this is kind of against how the LaTeX syntax works, but maybe there is a way around it.
With
\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\newcommand{\mycommand}[1][i]{\ensuremath{\frac{#1}{2}}}
\begin{document}
This \mycommand[1] is \mycommand, \mycommand and \mycommand.
\end{document}
I get
but I would like to get
without having to add {} behind the third \mycommand.


$\mycommand$– egreg Mar 29 '17 at 09:43