0

I want to replace - with --. I am able to replace \\ with -- using the following code:

{\def \\ {--\ } #3}

but if I try:

{\def - {--\ } #3} %Option 1
{\def- {--\ } #3} %Option 2
{\def-{--\ } #3} %Option 3
{\def - {{--\ }} #3} %Option 4

or any combination of the above, it causes

Missing control sequence inserted.
<inserted text>
                \inaccessible
    l.77         \begin
                   {rItemize}

in the part of the .tex file:

    \begin{rESubsection}
        {text}
        {text}
        {\def \\ {--\ }{Start Date \\ End Date}}
        \begin{rItemize} % here
            \item text
        \end{rItemize}
    \end{rESubsection}

ChatGPT did not help even if it gave a running code. I tried all above code on different compilers versions of pdflatex (2022, 2021, 2020) but to no avail.

Here is a full minimal working .tex file on pdflatex compiler version 2020 (legacy) on overleaf as per comments request.

\documentclass[11pt,letterpaper]{article} % Font size and paper type

\usepackage[parfill]{parskip} % Remove paragraph indentation

\usepackage{xparse} % used for any mmooo...

\usepackage{enumitem} % For rItemize left margin
\usepackage[left=0.4 in,top=0.4in,right=0.4 in,bottom=0.4in]{geometry} % Document margins
%\usepackage[margin=0.4in]{geometry}

\NewDocumentEnvironment{rSubsection}{mmmo}{
  \textbf{#1}, {#2}\IfValueT{#4}{, \textit{#4}}. \hfill {\def \\ {--\ } #3}
  \par
}{}

\NewDocumentEnvironment{rItemize}{
    \begin{itemize}
      \raggedright
}{
  \end{itemize}
}

\begin{document}
%----------------------------------------------------------------------------------------
% Professional Experience   
%----------------------------------------------------------------------------------------

    \begin{rSubsection}
        {Role}
        {Company Name}
        {Date - Date}
        [Location]
        \begin{rItemize}
            \item Text.
        \end{rItemize}

            \begin{rSubsection}
        {Role}
        {Company Name}
        {Date \\ Date}
        [Location]
        \begin{rItemize}
            \item Text.
        \end{rItemize}
    \end{rSubsection}
    \end{rSubsection}
\end{document}

This is the result: enter image description here

9
  • 3
    It's unlikely that ChatGPT is helpful in any case involving TeX: its answers are usually plainly wrong. Anyway, \def-{...} is not going to work. Please, add a full minimal example, because rESubsection is not a standard environment.
    – egreg
    Commented Aug 13, 2023 at 10:52
  • @egreg Hope this is what you asked for Commented Aug 13, 2023 at 11:13
  • 1
    you can't define a character unless it is active (like ~) and you can not (easily) make - active without breaking its use in numbers and lengths, or tikz syntax Commented Aug 13, 2023 at 11:41
  • 1
    just use your editor to fix the source, do not use tex definitions here Commented Aug 13, 2023 at 11:42
  • Your minimal example isn't correct: \NewDocumentEnvironment needs 4 arguments! In your example the second argument with the <arg spec> is missing (it can be empty for ritemize but it needs to be there otherwise LaTeX will complain about an invalid \begin as argument specification. Commented Aug 13, 2023 at 11:43

2 Answers 2

1

If you want to replace something define low level TeX macros using delimited arguments.

For example

\newcommand{\replaceminus}[1]{\doreplaceminus #1-\relax}

\newcommand{\docreplaceminus}{}% ensure that this helper macro isn't defined yet
\def\doreplaceminus#1-#2\relax{%
  \ifx\relax#2\relax
    #1%
  \else
    #1--\doreplaceminus#2\relax   % replace next - (or remove additional -)
  \fi}

The macro \replaceminus can then used inside your shown rSubsection environment replacement begin text to replace every - in the third argument \replaceminus{#3}.

The helper macro \doreplaceminus assumes an argument with a minus character token (non-active, category code other) and \relax as end of the argument. The macro \replaceminus is defined to provide these two tokens as default. In a first step it is checked if we reached these two tokens, i.e., argument #2 is empty, and we are finished. Otherwise the - is replaced by two minus character tokens and everything starts anew. (The macro uses recursion! To avoid input stack overflows if there are two many minus characters you have to use some well known tricks like \def\EndFi#1\fi{\fi #1} to make the macro expansion end recursive.)

Please be aware of spaces in your input and your macros/environment definitions! My macro \replaceminus doesn't add or remove spaces.

2

I'm not sure what's preventing you from inputting

date -- date

if you want that. Anyway, you can “normalize” the input.

\documentclass[11pt,letterpaper]{article} % Font size and paper type

\usepackage[parfill]{parskip} % Remove paragraph indentation

\usepackage{enumitem} % For rItemize left margin
\usepackage[left=0.4 in,top=0.4in,right=0.4 in,bottom=0.4in]{geometry} % Document margins

\NewDocumentEnvironment{rSubsection}{mmmo}{%
  \par
  \textbf{#1}, {#2}\IfValueT{#4}{, \textit{#4}}.\hfill\parsedates{#3}%
  \par
}{}

\NewDocumentEnvironment{rItemize}{}
  {\begin{itemize}\raggedright}
  {\end{itemize}}

\ExplSyntaxOn
\NewDocumentCommand{\parsedates}{m}
 {
  \tl_set:Nn \l_tmpa_tl { #1 }
  \regex_replace_once:nnN { \s* (\c{\\}|\-)+ \s* } {\ --\ } \l_tmpa_tl
  \tl_use:N \l_tmpa_tl
 }
\ExplSyntaxOff

\begin{document}
%----------------------------------------------------------------------------------------
% Professional Experience   
%----------------------------------------------------------------------------------------

% double hyphen with spaces
\begin{rSubsection}
  {Role}
  {Company Name}
  {Date -- Date}
  [Location]
  \begin{rItemize}
    \item Text.
  \end{rItemize}
\end{rSubsection}

% double hyphen with no spaces
\begin{rSubsection}
  {Role}
  {Company Name}
  {Date--Date}
  [Location]
  \begin{rItemize}
    \item Text.
  \end{rItemize}
\end{rSubsection}

% double hyphen with one space
\begin{rSubsection}
  {Role}
  {Company Name}
  {Date-- Date}
  [Location]
  \begin{rItemize}
    \item Text.
  \end{rItemize}
\end{rSubsection}

% double hyphen with one space
\begin{rSubsection}
  {Role}
  {Company Name}
  {Date --Date}
  [Location]
  \begin{rItemize}
    \item Text.
  \end{rItemize}
\end{rSubsection}

% single hyphen with spaces
\begin{rSubsection}
  {Role}
  {Company Name}
  {Date - Date}
  [Location]
  \begin{rItemize}
    \item Text.
  \end{rItemize}
\end{rSubsection}

% single hyphen with no spaces
\begin{rSubsection}
  {Role}
  {Company Name}
  {Date-Date}
  [Location]
  \begin{rItemize}
    \item Text.
  \end{rItemize}
\end{rSubsection}

% single hyphen with one space
\begin{rSubsection}
  {Role}
  {Company Name}
  {Date- Date}
  [Location]
  \begin{rItemize}
    \item Text.
  \end{rItemize}
\end{rSubsection}

% single hyphen with one space
\begin{rSubsection}
  {Role}
  {Company Name}
  {Date -Date}
  [Location]
  \begin{rItemize}
    \item Text.
  \end{rItemize}
\end{rSubsection}

% double backslash with spaces
\begin{rSubsection}
  {Role}
  {Company Name}
  {Date \\ Date}
  [Location]
  \begin{rItemize}
    \item Text.
  \end{rItemize}
\end{rSubsection}

% double backslash with no spaces
\begin{rSubsection}
  {Role}
  {Company Name}
  {Date\\Date}
  [Location]
  \begin{rItemize}
    \item Text.
  \end{rItemize}
\end{rSubsection}

% double backslash with one space
\begin{rSubsection}
  {Role}
  {Company Name}
  {Date\\ Date}
  [Location]
  \begin{rItemize}
    \item Text.
  \end{rItemize}
\end{rSubsection}

% double backslash with one space
\begin{rSubsection}
  {Role}
  {Company Name}
  {Date \\Date}
  [Location]
  \begin{rItemize}
    \item Text.
  \end{rItemize}
\end{rSubsection}

\end{document}

enter image description here

As you see, the output is always the same.

You must log in to answer this question.

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