2

I would like to use the following command in my LaTeX code

\newcommand{\newsym}[2]{\newcommand{#1}{\ensuremath{#2}}}

so that I can easily define new symbols that I often use in my equations, such as:

\newsym{\kTq}{\frac{k_BT}{q}}.

The intent being to reduce the amount of typing necessary to define new symbols and to enhance readability of the code.

The problem now is twofold:

  • TeXstudio dosn't realize my \newsym defines a new command and complains that the first argument (\kTq) is an uncrecognized command.
  • It also doesn't realize the second argument is eventually enclosed in an \ensuremath environment and complains math command outside math environment

As a result I have my \newsym definitions cluttered with bad-syntax highlighting, impeding the readability, which was the entire half of the purpose of defining \newsum in the first place.

Can I solve this dilemma by somehow adjusting the way TeXstudio does the error highlighting in this particular case? I don't wan't to disable the highlighting altogether; they are otherwise useful.

Or is there a smarter alternative to my definition of \newsym that avoids this problem?

  • you can ignore that, as this stems from autocompletion in TXS, or you can write a custom .cwl for TXS to recognize your custom commands. – naphaneal Jul 1 '16 at 14:28
  • 2
    why do you need \ensuremath here at all? do you use \kTq outside of math mode? – David Carlisle Jul 1 '16 at 14:38
  • Thanks naphaneal. the custom .cwl might be the solution I'm looking for. Could you elaborate on how to do that in a answer to the question I would then accept that answer as the solution if it works. – Glemi Jul 1 '16 at 14:51
  • David, without the \ensuremath, as is also the case when I use \newsym, teXstudio will highlight the math-mode commands in the new symbol's definition. It's true that the new symbol still works nonetheless and you are right that I don't use the new symbol outside math-mode but that wasn't the point. – Glemi Jul 1 '16 at 14:55
  • it was one of your two bulletpoints, but without \ensuremath the new command is just the same as \newcommand so you could use that and presumably your editor will be happy without any more work needed. – David Carlisle Jul 1 '16 at 16:28
2

How to create a custom .cwl-file for TXS:

  1. Open any ASCII-Text editor of your choice, e.g. vi, Notepad++. Alternatively: use TXS.
  2. Create and save a new text file with the file extension .cwl. If created for a package, the .cwl-file needs to be named in compliance to the package name.
  3. Add all commands you created as follows:

    #<author> <date> \<command>{<arguments>} \begin{<environmentname>} \end{<environmentname>}

  4. Save and copy or move your .cwl-file to the settings directory of TXS. For Windows 7 it is in %AppData%\texstudio\completion\user.

  5. Restart TXS, if necessary and goto Options -> Configure TeXstudio -> Completion. Check box of your .cwl-file.

# indicates comment line unless used within classification or with keywords. For more information refer to TXS manual section 4.13 or TXS FAQ .

Example:

example.cwl

#naphaneal/2016-07-01 \begin{my-code} \end{my-code}

  • 2
    In particular, for the above case add \newsym{cmd}{def}#d. – Tim Hoffmann Jul 2 '16 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.