0

Assume that main.tex is my main compiled latex file. The rest of them (as sub.tex) includes the list of def ones in order to arrange each chapter, subchapter etc. How can I use defined style in main.tex inside the sub tex files?

Below example does not work. However, if I just copy entire content of sub.text into main.text, there is no issue. Somehow, CStyle is not detected inside sub.tex. Any suggestios? Thanks

main.tex

\lstdefinestyle{CStyle}{
    backgroundcolor=\color{backgroundColour},   
    commentstyle=\color{mGreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{mGray},
    ...
}
\input{sub.tex}
\begin{document}
    \callDefined{}
\end{document}

sub.tex

\def\callDefined{
    \begin{lstlisting}[style=CStyle]
            uint8_t a = 0xAB;
    \end{lstlisting}
}
5

1 Answer 1

0

You cannot use the lstlisting-environment within a macro-definition/as component of a \def's definition-text.

This is because the lstlisting-environment expects to obtain the tokens belonging to it by reading and tokenizing things from the .tex-file while the category-code-régime is temporarily changed and not by having tokens inserted in the course of delivering the replacement-text of a macro-token whereby that replacement-text got tokenized under unchanged catcode-régime at the time of carrying out the \def-directive for defini8ng that macro-token.

The following, however, where the lstlisting-environment is not inside \def, does work:

sub.tex

\begin{lstlisting}[style=CStyle]
uint8_t a = 0xAB;
\end{lstlisting}

test.tex

\documentclass{article}
\usepackage{color}
\definecolor{backgroundColour}{RGB}{255,240,245}
\usepackage{listings}
\lstdefinestyle{CStyle}{
    backgroundcolor=\color{backgroundColour},   
    commentstyle=\color{mGreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{mGray},
}
\begin{document}
\input{sub.tex}
\end{document}

enter image description here


Alternatively, instead of \def you can implement your own definition-command which does read the definition-text in verbatim-catcode-régime and define the command to pass the stuff that was read in verbatim-catcode-régime to \scantokens. You need some \newlinechar=\endlinechar-trickery to get the linebreaks right. You need to insert a comment-character at the end of what is passed to \scantokens to make sure the last endline-character inserted by the \scantokens-routine won't yield an unwanted empty line. Could look like the following example. Be aware that indentation within lstlistings-environments and thus within the second argument of \MyDefVerbatim occurring in sub.tex does matter.

sub.tex

\MyDefVerbatim\callDefined{%
\begin{lstlisting}[style=CStyle]
uint7_t a = 0xAB;
 uint8_t a = 0xAB;
  uint9_t a = 0xAB;
\end{lstlisting}
}%

main.tex

% With TeXLive 2020 and newer, the xparse-package doesn't need to be
% loaded explicitly as most of its features are integrated into the
% LaTeX 2e-kernel. 
% With TeXLive 2019 and older, the xparse-package needs to be
% loaded explicitly.
% Seems currently (Novemer 16, 2021) overleaf uses
% TeXLive 2019 (legacy) by default.
\RequirePackage{xparse}

\NewDocumentCommand\MyDefVerbatim{m}{%
  \begingroup\catcode`\^^I=12 \innderdefineverbatim{#1}%
}%
\begingroup\catcode`\^^A=14 \catcode`\%=12 \csname @firstofone\endcsname{^^A
  \endgroup
  \NewDocumentCommand\innderdefineverbatim{m+v}{^^A
     \endgroup
     \NewDocumentCommand{#1}{}{^^A
       \begingroup\newlinechar=\endlinechar\scantokens{\endgroup#2%}^^A
     }^^A
  }^^A
}%

\documentclass{article}
\usepackage{color}
\definecolor{backgroundColour}{RGB}{255,240,245}
\usepackage{listings}
\lstdefinestyle{CStyle}{
    backgroundcolor=\color{backgroundColour},   
    commentstyle=\color{mGreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{mGray},
}
\input{sub.tex}
\begin{document}
\callDefined
\end{document}

enter image description here

3
  • Thanks for your effort. However, I could not compile it because of the part of \begingroup\catcode on overleaf. Also, I am not sure what is going on there really. The way I solved it is using \lstinputlisting and moving entire codes into .c files. In this case, I can call them in def. I do not like my solution which is not scalable. I would like to understand your alternative suggestion which is what I am looking for. Could you give me more detail on it, why does not work on overleaf side ? Thanks
    – ozturkib
    Nov 16, 2021 at 13:02
  • @ozturkib I just registered an account at overleaf and tested. I got error-messages. Viewing the raw logs I found that with overleaf TeX Live 2019 is in use, which is rather old. Thus the line %\RequirePackage{xparse} needs to be activated by removing % so that it looks like \RequirePackage{xparse}. This is because with TeXLive 2020 and newer features of xparse are integrated into the kernel while with TeXLive 2019 you still need to load that package. I activated \RequirePackage{xparse} and no more errors occurred. Nov 16, 2021 at 19:02
  • @ozturkib In order to provide more detail in a way which makes sense to you I need to know about your level of knowledge of TeX/LaTeX. If I can assume extensive knowledge, I can make the explanation short. If not, then I may also have to outline how TeX processes an input file/a .tex file, and in which order which stages of processing take place. This will be more extensive and not necessarily easy to read due to the amount of information presented, and will probably result in queries for which one could perhaps use e-mail as a communication medium. Nov 16, 2021 at 19:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .