获取字符串的一部分

获取字符串的一部分

我是 LaTeX 新手,想写一篇日记。我曾经使用过从多个目录中的文件生成日志。

我的主要问题是,在资源管理器中,将各种文件夹命名为 Jan/Feb/...Ago/Dec... 会使其看起来很糟糕(按字母顺序排列会使月份显示得很奇怪:Ago 然后 Dec 然后 Jan ...)所以我在文件夹前面加上了 01Jan 02Feb 等...

但我不想显示前缀数字,所以我想做类似的事情

\substring{01Jan}{3}{5}

这是我目前得到的结果,我收到以下错误:“\myRenamedMonth 的使用与其定义不匹配

\documentclass{tufte-book} % producing handouts and books according to the style of Edward R. Tufte and Richard Feynman.
\usepackage{lipsum}
\usepackage{tikz}       % Tikz is a powerful tool to create graphic elements in LATEX
\usepackage{xifthen}    % extended if then else commands.
\usepackage{stringstrings} % \substring{abcdefgh}{3}{6} gives me the 3rd to 6th char = "cdef"

\newenvironment{loggentry}[2]% date, heading
{\noindent\textbf{#2}\marginnote{#1}\par}{\vspace{0.5cm}}

\def\?#1{}

\pgfmathtruncatemacro{\StartYear}{2017}
\pgfmathtruncatemacro{\EndYear}{2018}

\newcommand{\writetitle}{0}
\newcommand{\mytitle}[1]
{   \ifthenelse{\writetitle=1}{#1}{}
}

\newread\mysource

\begin{document}

\foreach \Year in {\StartYear,...,\EndYear}
{   \foreach \Month in {01Jan,02Fev,03Mar,04Abr,05Mai,06Jun,07Jul,08Ago,09Set,10Out,11Nov,12Dez}
    {   \foreach \Day in {01,02,03,04,05,06,07,08,09,10,...,31}
        {   \IfFileExists{\Year/\Month/\Day}
                {   \openin\mysource=\Year/\Month/\Day.tex
                    \read\mysource to \firstline
                    \closein\mysource
                    \xdef\writetitle{1}
                    \def \myRenamedMonth=\substring{\Month}{3}{6}
                    \begin{loggentry}{\Year - \myRenamedMonth - \Day}{\firstline}    
                        \xdef\writetitle{0}
                        \input{\Year/\Month/\Day}
                    \end{loggentry} 
        }
        {   % files does not exist, so nothing to do

        }

        }  
    }
}


 \end{document}

编辑:问题已通过使用 Skillmon 指出的正确语法得到解决(请将其作为答案提交,以便我接受它:),但又引发了另一个问题...第二个破折号“-”被“吃掉”并打印:

2018 年 1 月 3 日

而不是 2018 年 1 月 3 日

答案1

您使用了错误的语法来定义\myRenamedMonth。正确的是:(\def\myRenamedMonth{\substring{\Month}{3}{6}}尽管 01Jan 只有 5 个字符,所以应该是\substring{\Month}{3}{5})。

我建议的简单方法不会因删除第二个破折号而受到影响。还会\Year - \myRenamedMonth - \Day占用破折号前面的空格。正确的方法是\Year\ - \myRenamedMonth\ - \Day,但也许你--也应该使用不同的分隔符(也许 ?)。

另外,您不应在\newcommandand等的左括号后添加空格。这些确实会在您的输出中产生空格,这很可能是不需要的。此外,如果您不想在行末的右括号后添加空格,\foreach请确保在行末的右括号后添加 a 。%

希望下面的方法可以删除所有不需要的空格。它也能达到你想要的效果,而且不会像我的方法那样出现删除破折号的问题(为了确保万无一失,我使用了一些保护措施)。

\documentclass{tufte-book} % producing handouts and books according to the style of Edward R. Tufte and Richard Feynman.
\usepackage{lipsum}
\usepackage{tikz}       % Tikz is a powerful tool to create graphic elements in LATEX
\usepackage{xifthen}    % extended if then else commands.
%\usepackage{stringstrings} % \substring{abcdefgh}{3}{6} gives me the 3rd to 6th char = "cdef"

\newenvironment{loggentry}[2]% date, heading
{\noindent\textbf{#2}\marginnote{#1}\par}{\vspace{0.5cm}}

\def\?#1{}

\pgfmathtruncatemacro{\StartYear}{2017}
\pgfmathtruncatemacro{\EndYear}{2018}

\newcommand{\writetitle}{0}
\newcommand{\mytitle}[1]
  {\ifthenelse{\writetitle=1}{#1}{}}

\makeatletter
\long\def\removeFirstTwo#1%
  {%
    \if\relax\detokenize{#1}\relax%just to make sure the argument isn't empty
      \expandafter\@gobble
    \else
      \expandafter\@firstofone
    \fi
    {\expandafter\removeFirstTwo@i#1\q}%
  }
\long\def\removeFirstTwo@i#1#2#3\q{#3}
\makeatother

\newread\mysource

\begin{document}

\foreach \Year in {\StartYear,...,\EndYear}
    {\foreach \Month in {01Jan,02Fev,03Mar,04Abr,05Mai,06Jun,07Jul,08Ago,09Set,10Out,11Nov,12Dez}
        {\foreach \Day in {01,02,03,04,05,...,31}
            {\IfFileExists{\Year/\Month/\Day}
                {%
                  \openin\mysource=\Year/\Month/\Day.tex
                  \read\mysource to \firstline
                  \closein\mysource
                  \def\writetitle{1}% no need for xdef here
                  \xdef\myRenamedMonth{\removeFirstTwo{\Month}}%
                  \begin{loggentry}{\Year\ - \myRenamedMonth\ - \Day}
                    {\firstline}%
                    \def\writetitle{0}% no need for xdef here
                    \input{\Year/\Month/\Day}
                  \end{loggentry}%
                }
                {% files does not exist, so nothing to do
                }
            }%
        }%
    }%
 \end{document}

编辑:您可能还想将您的内容更改\mytitle为类似以下内容:

\newcommand{\mytitle}
  {
    \ifnum\writetitle=1
      \expandafter\@firstofone
    \else
      \expandafter\@gobble
    \fi
  }

或者改为使用布尔值(\newif\ifwritetitle然后\writetitletrue\writetitlefalse翻转它们,而不是\ifnum\writetitle=1仅仅使用\ifwritetitle)。

这里没必要\ifthenelse

答案2

线路

\def \myRenamedMonth=\substring{\Month}{3}{6}

恐怕这是完全错误的。

由于您的名称遵循明确定义的方案,因此您可以使用两步方法:添加

\makeatletter
\newcommand{\getRenamedMonth}{%
  \edef\myRenamedMonth{\expandafter\@gobbletwo\Month}%
}
\makeatother

在序言中并用它\getRenamedMonth代替那条错误的线。

\documentclass{tufte-book} % producing handouts and books according to the style of Edward R. Tufte and Richard Feynman.
\usepackage{lipsum}
\usepackage{tikz}       % Tikz is a powerful tool to create graphic elements in LATEX
\usepackage{xifthen}    % extended if then else commands.

\newenvironment{loggentry}[2]% date, heading
{\par\noindent\textbf{#2}\marginnote{#1}\par}{\vspace{0.5cm}}

\def\?#1{}% what for?

\newcommand{\StartYear}{2017}
\newcommand{\EndYear}{2018}

\newcommand{\writetitle}{0}
\newcommand{\mytitle}[1]{%
  \ifthenelse{\writetitle=1}{#1}{}%
}

\newread\mysource

\makeatletter
\newcommand{\getRenamedMonth}{%
  \edef\myRenamedMonth{\expandafter\@gobbletwo\Month}%
}
\makeatother

\begin{document}

\foreach \Year in {\StartYear,...,\EndYear}{%
  \foreach \Month in {01Jan,02Fev,03Mar,04Abr,05Mai,06Jun,07Jul,08Ago,09Set,10Out,11Nov,12Dez}{%
    \foreach \Day in {01,02,03,04,05,06,07,08,09,10,...,31}{%
      \IfFileExists{\Year/\Month/\Day}
        {\openin\mysource=\Year/\Month/\Day.tex
         \read\mysource to \firstline
         \closein\mysource
         \gdef\writetitle{1}%
         \getRenamedMonth
         \begin{loggentry}{\Year - \myRenamedMonth - \Day}{\firstline}    
           \gdef\writetitle{0}
           \input{\Year/\Month/\Day}
         \end{loggentry}%
        }
        {}% IfFileExists
    }%
  }%
}

\end{document}

相关内容