2

This problem has been bugging me for ages, and I haven't been able to fix it. When writing papers, I like to use \[ \] rather than \begin{equation} \end{equation}. Similarly, for aligned equations, I have defined the following environment:

\def\<#1\>{\begin{align}#1\end{align}}

The command works fine, and compiles in TexStudio without issue. However, all code inside of \< \> is highlighted red as TexStudio does not recognise it as a math environment. How can I amend this?

  • Hi @tomasliam and welcome to TeX-SE. Normally I define my own commands with \newcommand. When I define them locally, TeXstudio always recognize them. When I define them in an own package, I need a .cwlfile so that the \newcommands are recognized. I don't use \def, [check this] (https://tex.stackexchange.com/q/655/140133) except when it is the only option. Personally, I would not suggest creating a command to encapsulate an environment. Environments are well recognizable in TeXstudio, simple to identify and the default shortcut Ctrl + E is very useful. – FHZ Mar 31 '20 at 03:30
  • Yeah most of my commands I define with \newcommand and there isn't an issue. However, I cannot define environments with \newcommand, and require \def. Hmmm... I understand your point, and might go back to just using the long form, I just use align environments so often I would like a simpler version. I did not know of that shortcut, I'll give it a go. –  Mar 31 '20 at 03:47
  • Oh I see, you may try newenvironment, newtheorem (needs amsthm). Another VERY good idea to speed up the writing process is to create TeXstudio macros, I really enjoy them (I have macros for predefined blocks of syntax to figures, tables, equations, listings, headers). It is faster and keep the basic syntax neat and tidy. – FHZ Mar 31 '20 at 04:04

1 Answers1

0
\documentclass{article}
\usepackage{amsmath}
\def\<#1\>{\begin{align}#1\end{align}}
% ==========
\def\bla#1{
    \begin{align}
        \textrm{POTATO: }{#1}
    \end{align}
}
% ==========
\begin{document}
\<x = 2\>
\bla{x = 2}
Text.

\bla{x = 4}

Text.
\bla{x = 6}
\begin{align}\label{eq:A}
x = 2 \\
x = 4 \\
x = 6
\end{align}

\bla{\label{eq:B} x = 8}
%\bla{\label{eq:C} x = 8 \\ x = 10}

\eqref{eq:A}, \eqref{eq:B}. % \eqref{eq:C}.
\end{document}

When you define a name for your newcommand, it will be recognized at TeXstudio.

enter image description here

autocompletion/autosuggestion works nice.

enter image description here

and, as you said in the question, it works

enter image description here enter image description here

On the other hand, if you keep environments, you keep some useful tools and flexibility.

enter image description here

It shrinks. (And I personally love it).

enter image description here

It also applies for personal commands with more than one line

enter image description here

But, if you don't think carefully (As I did when trying to recreating the situation) you might have more headaches in the future than you expected.

enter image description here

Oh... and don't get me wrong, I really like to create my own commands

enter image description here

When customizing, you should really try to find a balance (trade-off) between flexibility, readability, usability and organization (and a setup that is a dream for someone might the a nightmare for someone else). The overall suggestion is to look for good practices.

FHZ
  • 3,939