The Community code formating is a good balance between displaying code and being able to copy-paste it without errors into Mathematica or Wolfram Cloud notebook. For most of the cases Community code formating here does a good job. For the cases when more math-like formating is needed we recommend also (in-addition) using an image, as you did yourself. Or you can use MathJax and TeXForm and symbol $:
I guess why the former is preferred over the latter is because the former is copy-pasteable, while the latter is not.
You can however add formulas in TeX: