我正在使用该newfile
包使用输入文件插入图形、标题等。一切正常,但变量后会创建一个空格,如果该变量表示必须包含在 tex 文件中的图形名称,则会出现问题。这是我的代码:
\documentclass[12pt]{article} % Default font size is 12pt, it can be changed here
\usepackage{geometry} % Required to change the page size to A4
\geometry{a4paper} % Set the page size to be A4 as opposed to the default US Letter
\usepackage{graphicx} % Required for including pictures
\usepackage{float} % Allows putting an [H] in \begin{figure} to specify the exact location of the figure
\usepackage{wrapfig} % Allows in-line images such as the example fish picture
\usepackage{newfile}
\linespread{1.2} % Line spacing
%\setlength\parindent{0pt} % Uncomment to remove all indentation from paragraphs
\graphicspath{{Pictures/}} % Specifies the directory where pictures are stored
\begin{document}
%Define the file read object
\newread\productin
%Open auxillary file and stream relevant variables
\immediate\openin\productin=aux_files/femara.tex %this filename must change according to relevant report
\newcounter{linenr}
\loop\unless\ifeof\productin
\stepcounter{linenr}
\immediate\read\productin t\expandafter o\csname var\alph{linenr}\endcsname
\repeat
\immediate\closein\productin
%----------------------------------------------------------------------------------------
% TITLE PAGE
%----------------------------------------------------------------------------------------
\begin{titlepage}
\newcommand{\HRule}{\rule{\linewidth}{0.5mm}} % Defines a new command for the horizontal lines, change thickness here
\center % Center everything on the page
\HRule \\[0.8cm]
{ \huge \bfseries \varc Analytics Report for \vara}\\[0.4cm] % Title of your document
\HRule \\[2cm]
\textsc{\LARGE \varb}\\[12cm] % Report month
\begin{minipage}{1\textwidth}
\begin{flushright} \large
\emph{Prepared by:}\\
\textsc{Umami Consulting} \\[0.4cm]
{\large \today}\\[3cm]
\end{flushright}
\end{minipage}
%\includegraphics{Logo}\\[1cm] % Include a logo - this will require the graphicx package
\vfill % Fill the rest of the page with whitespace
\end{titlepage}
%----------------------------------------------------------------------------------------
% TABLE OF CONTENTS
%----------------------------------------------------------------------------------------
\tableofcontents % Include a table of contents
\newpage % Begins the essay on a new page instead of on the same page as the table of contents
%----------------------------------------------------------------------------------------
% REPORT OVERVIEW
%----------------------------------------------------------------------------------------
\section{Report Overview} % Major section
\subsection{Subsection 1} % Sub-section
\begin{figure}[H] % Example image
\center{\includegraphics[width=1\linewidth]{\vard}}
\caption{Example image.}
\label{fig:speciation}
\end{figure}
%----------------------------------------------------------------------------------------
% CONCLUSION
%----------------------------------------------------------------------------------------
\section{Conclusion} % Major section
%----------------------------------------------------------------------------------------
% BIBLIOGRAPHY
%----------------------------------------------------------------------------------------
\begin{thebibliography}{99} % Bibliography - this is intentionally simple in this template
\bibitem[Figueredo and Wolf, 2009]{Figueredo:2009dg}
Figueredo, A.~J. and Wolf, P. S.~A. (2009).
\newblock Assortative pairing and life history strategy - a cross-cultural
study.
\newblock {\em Human Nature}, 20:317--330.
\end{thebibliography}
%----------------------------------------------------------------------------------------
\end{document}
输入文件内容(femara.tex
)
June 2014
Data
Plt_Province
我的问题是\vara
产生Area
而不是Area
。我的输入文件的变量名后没有空格。
知道如何去除空白吗?
答案1
尾随空格是由于输入文件中的行尾字符造成的。
最简单的解决方法是通过设置(临时)将其完全删除
\endlinechar=-1
这是一个完整的示例,我删除了不相关的内容,但为下划线添加了安全措施(您可能不需要它)。我还更改了输入文件,以\jobname.dat
避免破坏我自己的文件。
请注意,这\immediate
仅对于写出文件才有意义,对于读取文件则没有意义。
\documentclass{article}
\usepackage[T1]{fontenc} % for printing the underscore
\newcounter{linenr}
\begin{document}
%Define the file read object
\newread\productin
%Open auxiliary file and stream relevant variables
\openin\productin=\jobname.dat % <--------- use the real name here
\chardef\savedendlinechar=\endlinechar\endlinechar=-1
\chardef\saveduscatcode=\catcode`\_ \catcode`\_=12 % protect the underscore
\setcounter{linenr}{0}
\loop\unless\ifeof\productin
\stepcounter{linenr}
\read\productin to \next
\expandafter\let\csname var\alph{linenr}\endcsname\next
\repeat
\closein\productin
\endlinechar=\savedendlinechar % restore the endlinechar
\catcode`\_=\saveduscatcode % restore the usual underscore
\vara
\varb
\varc
\end{document}
这是一个expl3
允许更改前缀的实现
\documentclass{article}
\usepackage[T1]{fontenc} % for printing the underscore
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\readfile}{ O{var} m}
{
\alta_readfile_to:nn { #1 } { #2 }
}
\int_new:N \l_alta_readfile_line_int
\ior_new:N \g_alta_readfile_file_stream
\cs_new_protected:Npn \alta_readfile_to:nn #1 #2
{
\int_zero:N \l_alta_readfile_line_int
% link the input stream to the file
\ior_open:Nn \g_alta_readfile_file_stream { #2 }
% loop through the input stream
\ior_map_inline:Nn \g_alta_readfile_file_stream
{
% step the line counter
\int_incr:N \l_alta_readfile_line_int
% define '#1a', '#1b' and so on (default for #1 is var)
\tl_set:cn
{
#1\int_to_alph:n { \l_alta_readfile_line_int }
}
{ \tl_to_str:n { ##1 } } % the input line is stringified
}
}
\ExplSyntaxOff
\begin{document}
\readfile{\jobname.dat}
\vara
\varb
\varc
\readfile[foo]{\jobname.dat}
\fooa
\foob
\fooc
\end{document}