2

I want to create a command to simplify using the command lstinputlisting from the package listings, which can accept a single parameter as the file path and pass it to both the caption and path parameter of lstinputlisting. In one word, use the file path as the caption.

Consider the following LaTeX source:

\documentclass[UTF8]{ctexart}
\usepackage{listings}

\newcommand{\myincludecode}[1]{\lstinputlisting[caption=#1, language=matlab]{#1}}
\newcommand{\mysecondincludecode}[2]{\lstinputlisting[caption={#2}, language=matlab]{#1}}


\begin{document}

\myincludecode{main.m}                        % line 10
\myincludecode{gen_data.m}                    % line 11

\mysecondincludecode{main.m}{main.m}          % line 13
\mysecondincludecode{gen_data.m}{gen\_data.m} % line 14

\end{document}

Clearly, the commands in line 13 & 14 work well, which both correctly include the corresponding file and print the corresponding captions.

The line 10 also works well. However, the line 11 includes the corresponding file but outputs no caption. The log file says:

Try.tex|11 error| Missing $ inserted.
Try.tex|11 error| Extra }, or forgotten $.
Try.tex|11 error| Missing $ inserted.
Try.tex|11 error| Missing } inserted.

It's obvious that the underscore breaks down my command. So, I wonder how to modify myincludecode to make it work - even when meeting some special characters, such as the underscore here.

3

Detokenize the argument:

\documentclass[UTF8]{ctexart}
\usepackage[T1]{fontenc}
\usepackage{listings}

\newcommand{\myincludecode}[1]{\lstinputlisting[caption=\detokenize{#1}, language=matlab]{#1}}

\begin{document}

\myincludecode{main.m}
\myincludecode{gen_data.m} 

\end{document}

enter image description here

| improve this answer | |
1

In mathmode underscore has a function. It changes the next character into subscript. Hence it expects a $ sign. This is a simple solution to your problem.

\documentclass[UTF8]{ctexart}
\usepackage{listings}

\begin{document}
\begingroup
\newcommand{\myincludecode}[1]{\catcode`_=11\lstinputlisting[caption=#1, language=matlab]{#1}}
\myincludecode{main.m}
\myincludecode{gen_data.m}
\endgroup
$1_2$
\end{document}

Catcode means category code. Category of _ is 8 which assigns some function to it, if I change it to 11, it changes underscore to a letter category, which is probably what you want. Adding this command between \begingroup & \endgroup makes the subscript function intact outside it's scope.

| improve this answer | |
-1

You must avoid “_” in file names and in cite or ref tags, or you must use the babel package, with its active-character controls, or you must give the [strings] option, which attempts to redefine several commands (and may not work perfectly). Even without the [strings] option or babel, you can use occasional underscores like: “\include{file\string_name}”.

The default operation is quite simple and needs no customization; but you must avoid using “_” in any place where LaTeX uses an argument as a string of characters for some control function or as a name. These include the tags for \cite and \ref, file names for \input, \include, and \includegraphics, environment names, counter names, and placement parameters (like [t]). The problem with these contexts is that they are ‘moving arguments’ but LaTeX does not ‘switch on’ the “\protect mechanism” for them.

If you need to use the underscore character in these places, the package option [strings] is provided to redefine commands that take such a string argument so that protection is ap- plied (with \protect made to be \string). The list of commands this provision affects is given in \UnderscoreCommands, with \do before each one; plus several others covering \input, \includegraphics, \cite, \ref, and their variants.

Please see--http://ctan.imsc.res.in/macros/latex/contrib/underscore/underscore.pdf and--Listing of a file with underscores in its name/path

enter image description here enter image description here

| improve this answer | |
  • So let's assume the OP uses underscores in their filenames. How can your answer be used to address that issue? Can you provide an example of that where the same argument is used for the caption and the actual file in \lstinputlisting? – Werner Nov 27 '19 at 5:21
  • @Werner wanted an example the same has been incorporated – js bibra Nov 27 '19 at 5:28
  • I'll emphasize my comment request: Can you provide an example of where the same argument is used for the caption and the actual file in \lstinputlisting? test\_file.c is different from test_file.c. – Werner Nov 27 '19 at 5:31
  • Still learning Sir Still learning --way behind to challenge your rep of 475,298 REPUTATION – js bibra Nov 27 '19 at 5:35

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.