# TeXStudio doesn't recognize self-defined commands in external file

I'd like to define some tables as commands (MWE) in an external .tex file, because there are a lot of settings for them. This external file gets included in the main document by the \input{} command. Using my pre-defined commands is no problem. TeXStudio is able to compile the main document and also gives an output. The problem is, that TeXStudio doesn't recognize the \hline command and the & separator for tables inside my self-defined table (the editor says there are table commands outside of table environment). Also, if only the main document is opened in the editor (without the myTemplate file), the \mytable{} and \lb commands I defined in the external document are not recognized.

Is there a way to make this red highlighting disappear without simply deactivating the syntax-highlighting? I got some help from here: TeXStudio doesn't recognize some commands, because I've had the same Problem with other commands before. Though, These commands were from packages I got from ctan, so no self-defined commands.

I'm not sure if this problem is reproducable for you, but here is a MWE:

\documentclass[]{scrartcl}
\input{myTemplate.tex}

\begin{document}

\mytable{
%% \hline A & B & C & D & E \\ \hline
a & b1 \lb b2 \lb b3 & c1 \lb c2 \lb c3 & d & e \\ \hline
& & & & \\ \hline
& & & & \\ \hline
}

\end{document}


And the myTemplate file:

\usepackage{tabularx}
\usepackage{here}
\usepackage{array}
\usepackage[table]{xcolor}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}

\newcommand{\mytable}[1]{%
\begin{center}
\rowcolors{1}{white}{pink}
\begin{tabular}{| M{.2\textwidth} | M{.15\textwidth} | M{.1\textwidth} | M{.2\textwidth} | M{.2\textwidth} | M{.15\textwidth}|} \hline
A & B & C & D & E \\ \hline
#1
\end{tabular}
\end{center}%
}

\newcommand{\lb}{\linebreak}

• it won't affect your highlighting but why have you got \begin{table} in your definition of \mytable? as you haven't got a caption it would be better and simpler just to use tabular. Also you are missing % after the { and after \end{tabular} – David Carlisle Mar 23 '16 at 7:37
• In the real document the table should have alternating rowcolors. So there is a rowcolors command, which must be put inside a table environment, I think ... But I added this Information and also the %, thanks for the advice! – kanra Mar 23 '16 at 8:20
• \rowcolors doesn't require table – David Carlisle Mar 23 '16 at 8:52
• If I place \rowcolors outside a table environment all tables would get these rowcolors, wouldn't they? But I'd like to have this setting only in this particular kind of tables - is this possible without putting it inside the table environment? – kanra Mar 23 '16 at 9:12
• you could use center environment or just {..} or any other group, it is nothing to do with table – David Carlisle Mar 23 '16 at 9:22

It's not possible to selectively switch of parts of the syntax checking. It's also not possible to make the parser accept table-like behavior in arguments.

However, you could define your own table environment:

\newenvironment{mytable}{%
\begin{center}
\rowcolors{1}{white}{pink}
\begin{tabular}{| M{.2\textwidth} | M{.15\textwidth} | M{.1\textwidth} | M{.2\textwidth} | M{.2\textwidth} | M{.15\textwidth}|}     \hline
A & B & C & D & E \\ \hline
}{\end{tabular}\end{center}}


in a custom .cwl declare that environment to be tabular-like:

\begin{mytable}#\tabular
\end{mytable}


and use it in your document:

\begin{mytable}
%% \hline A & B & C & D & E \\ \hline
a & b1 \lb b2 \lb b3 & c1 \lb c2 \lb c3 & d & e \\ \hline
& & & & \\ \hline
& & & & \\ \hline
\end{mytable}

• Thanks for your answer! I defined the newenvironment in the myTemplate.tex. Then I created a .cwl file (where the other .cwl files are stored) containing the suggested lines and used the mytable Environment in my main document. In Addition, I tried the same with an .sty file instead of a .tex file. Also, I activated the myTemplate.cwl in the completion section of the TeXStudio configurations. Though, the & and \hline are still said to be tabular commands outside of table Environment. For the \end{} command it says "command not recognized". Did I do anything wrong? – kanra Mar 24 '16 at 7:51
• Just from what you are writing, it should work. Of course, I cannot see your setup. Try fist with a minimal document in a single file that defines and uses the envrionment. – Tim Hoffmann Mar 25 '16 at 14:10

I had this problem. I have three files:

• MySty.sty (here defined some new commands, enviroments, etc),
• print.tex
        \documentclass[12pt]{article}
\usepackage{MyStyle.sty}

\begin{document}
\input{1.tex}
\input{2.tex}
.............
\end{document}

• 1.tex (here my custom commands not recognized by TeXStudio)

I just changed \usepackage{MyStyle.sty} to \input{MyStyle.sty}. And now all project succesly compiling and there's no unrecognizing highlighting in all my files.

• In the question \input was already used, and not \usepackage, so this is not really an answer to the question. – Marijn Sep 20 at 19:00