2

I know that using article to obtain chapters is idiotic, but the code to improve is long and depends on this.

In the following code I define a new macro \Section*, but I must remove curly braces from the original argument of \section*.

\documentclass{article}

\let\oldsection\section \def\Section\huge CHAPTER #1. \ #2.{\section{{\huge #1 $|$} \Large #2 \hfill}}

\begin{document}

\section*{\huge CHAPTER 1. \ Title}

Some text

\Section*\huge CHAPTER 2. \ Title.

\end{document}

Is it possible to redefine \Section* (or maybe even \section*) to use it simply as \Section*{The text as in the pattern}?

enter image description here

  • Wouldn't it be simpler to define \chapter? And why the weird input syntax? – egreg Oct 05 '23 at 09:19
  • @egreg As I wrote, I am improving someone's idiotic code, where arguments are, say, \large, the word Chapter, \\\, and so on, as in the MWE. Giving the proper structure requires going through over 100 pages of text. – Przemysław Scherwentke Oct 05 '23 at 09:22
  • Just to be sure: you basically want \Section*{\huge CHAPTER 2. \\ Title} to work, right? – campa Oct 05 '23 at 09:27
  • @campa Yes, instead of removing curly braces and giving a dot as the end of the last argument. – Przemysław Scherwentke Oct 05 '23 at 09:28
  • why define \Section so it has to have a* ? – David Carlisle Oct 05 '23 at 09:29
  • @DavidCarlisle Because the code to improve is a total mess. In particular, "chapters" are obtained with numbers in the title. (Horrible, I know.) – Przemysław Scherwentke Oct 05 '23 at 09:32
  • 5
    It would be better to spend 10 minutes applying regex to the source to fix the markup than 10 days writing tex code to make the existing source work. – David Carlisle Oct 05 '23 at 09:33
  • @PrzemysławScherwentke Please, show a specific call done in the idiotic code. – egreg Oct 05 '23 at 09:33
  • @egreg The chapters begin with something like this \section*{\huge CHAPTER 1.\\ Introduction} \addcontentsline{toc}{section}{\numberline{}CHAPTER 1. Introduction} \setcounter{section}{1}. I have changed the beginning as in MWE. – Przemysław Scherwentke Oct 05 '23 at 09:37
  • 2
    "a total mess" I understand. But how many chapters are there? I would think it would be 5 minutes to ctrl-f "CHAPTER" and change those lines. Or 10 minutes to figure out the regex. Or an hour to figure out the tex. – Teepeemm Oct 05 '23 at 14:36
  • @Teepeemm You are probably right. Is there a regex manual for beginners with application to TeX/LaTeX? – Przemysław Scherwentke Oct 05 '23 at 20:44
  • 1
    Your editor should provide regex support, though it may use a modified syntax. (If it doesn't, get another editor!) – cfr Oct 06 '23 at 01:25

4 Answers4

7

Strange as it may seem, there are times when TeX isn't the answer.

Given an original document

\documentclass{article}

\begin{document}

\section*{\huge CHAPTER 1.\ Introduction} \addcontentsline{toc}{section}{\numberline{}CHAPTER 1. Introduction} \setcounter{section}{1}

\section{This is a normal section}

\section*{\huge CHAPTER 2.\ A title} \addcontentsline{toc}{section}{\numberline{}CHAPTER 2. A title} \setcounter{section}{1}

\section{This is a normal section}

\end{document}

Rather than adding yet more weird code to try to customise this, make a small edit to fix the source, I'll use sed here but you could use perl or your editor or anything else with regular expression replace.

hmm.sed

s/{article}/{report}/
s/\\section.{\\huge CHA.*\\\\ /\\chapter{/g
/\\addcontentsline.*CHAPTER/ d
/\\setcounter{section}{1}/ d

Then running

sed -f hmm.sed file.tex > file2.tex

Produces

\documentclass{report}

\begin{document}

\chapter{Introduction}

\section{This is a normal section}

\chapter{A title}

\section{This is a normal section}

\end{document}

So a clean, normal report class document with numbered chapters. You can further customise the display of chapters if required, either directly or using one of the many packages for that.

David Carlisle
  • 757,742
2

This is a modified recommendation based on David's answer.

sed is great, but I worry the proposed recipe is insufficiently cautious. Given the state of the source, do you really trust the author to have ensured the code is consistently formatted throughout the document? Perhaps that's true, but I think it is safer - at least when I'm the author - to assume fallibility.

s/{article}/{report}/

Fine. This is searching for an exact string and replacing it. On the other hand, it's probably only going to replace a single word in the document. So it's very low risk, but very low benefit.

s/\\section.{\\huge CHA.*\\\\ /\\chapter{/g

Are we sure we don't have something like

\section*{\huge CHAPTER 4. Another Thing

where the author forgot the line break? Or

\section {\huge CHAPTER 5. \ Yet Another

where the author missed the second backslash? If the script simply fails to find those things, that's not so bad. But

\section*{\huge CHAPTER 4. Another Thing\\And Another

will lose the first part of the title.

I'd try something more like

s/\\section\*{\\huge CHAPTER [0-9][0-9]*\. *\\\\ /\\chapter{/g

which is much more targeted.

However, what really motivated this answer is the third and fourth lines.

/\\addcontentsline.*CHAPTER/ d
/\\setcounter{section}{1}/ d

If the author is consistent, this is, of course, just what you want. But if not, you could potentially discard chunks of content unawares. You still have the original file1.tex, but I'd still err more on the side of caution.

Suppose the file contains

\section*{\huge CHAPTER 10.\\ Vital}\addcontentsline{toc}{section}{\numberline{}CHAPTER 10. Vital}
\setcounter{section}{10}       This is an especially vital part of the text, so it's a shame this will get lost.

By this point, the first bit of mess is gone, so we have

\chapter{Vital}\addcontentsline{toc}{section}{\numberline{}CHAPTER 10. Vital}
\setcounter{section}{10}       This is an especially vital part of the text, so it's a shame this will get lost.

These lines now become

i.e. nothing at all. Moreover, you might have something like

\addcontentsline{toc}{section}{\numberline{}CHAPTER 11.
 Crucial}
\setcounter{section}{11}       

which will become

 Crucial}

and result in unbalanced text.

Instead, I'd recommend commenting these lines. This has the disadvantage of adding (hopefully unnecessary) clutter, but you can easily delete the lines wherever they become annoying, because you can just visually check it's safe. And if you never look at them to become annoyed, they will do no harm. On the other hand, it will be easier to figure out what's happened if something seems to be missing or you suddenly get weird errors.

/\\addcontentsline.*CHAPTER/s/^/%/
/\\setcounter{section}{1}/s/^/%/

Altogether,

s/{article}/{report}/
s/\\section\*{\\huge CHAPTER [0-9][0-9]*\. *\\\\ /\\chapter{/g
/\\addcontentsline.*CHAPTER/s/^/%/
/\\setcounter{section}{1}/s/^/%/

Especially if the document is fairly long, I'd prefer this because, however careful I am, I might not realise sed removed too much for some time and it is not always easy, at that point, to unpick what's happened. If the document is short, however, you can probably use David's version and just diff to check for anything untoward.

diff file1.tex file2.tex

For more context

diff -Naur file1.tex file2.tex

This latter invocation is often used to make patches and can be helpful if the changes don't make sense in isolation.

cfr
  • 198,882
  • in a real case I would certainly clean the source rather than add more tex, but I might not use sed. doing the same regex replace in an editor such as emacs as a query+replace allows you to confirm each change with a single key, do you can stop and edit by hand or modify the regex if it is not matching cleanly – David Carlisle Oct 06 '23 at 06:06
  • Another interesting solution. +1. – Przemysław Scherwentke Oct 06 '23 at 06:22
  • @DavidCarlisle I'd use Kile or vim, but yes. But if I'm providing somebody unfamiliar with sed with a sed script, I'd rather provide them with a more cautious one. And I took that to be what your answer was doing. – cfr Oct 06 '23 at 17:04
1

If (and that's a big if) I understand, this could be a way with the good old Grab to #{ macro arguments

\documentclass{article}

\def\Section#1#{\SectionA}% fake first argument to ignore spaces after \def\SectionA#1{\SectionB#1\relax} \def\SectionB\huge CHAPTER #1. \ #2\relax{\section*{{\huge #1 $|$} \Large #2 \hfill}}

\begin{document}

\section*{\huge CHAPTER 1. \ Title}

Some text

\Section*{\huge CHAPTER 2. \ Title} Some other text.

\end{document}

enter image description here

But I second David's comment that a regex search/replace might be a better strategy.

campa
  • 31,130
1

Let's look at the code you show in comments (you should have reported it in the question):

\section*{\huge CHAPTER 1.\\ Introduction}
\addcontentsline{toc}{section}{\numberline{}CHAPTER 1. Introduction}
\setcounter{section}{1}

You seem to want to modify it in order to apply a different formatting. I'll assume there are no other instances of \section* except for setting these “chapters”.

\documentclass{article}

\NewCommandCopy{\latexsection}{\section}

\RenewDocumentCommand{\section}{sO{#3}m}{% \IfBooleanTF{#1}{\fakechapter{#3}}{\latexsection[#2]{#3}}% }

\newcommand{\fakechapter}[1]{\makefakechapter#1\makefakechapter} \def\makefakechapter #1CHAPTER #2.#3\makefakechapter{% \makefakechapteraux{#2}#3\makefakechapter } \def\makefakechapteraux#1#2\#3\makefakechapter{% \latexsection*{{\huge\ignorespaces#1\unskip\ $|$\ }{\Large\ignorespaces #3}}% }

\begin{document}

\section*{\huge CHAPTER 1.\ Introduction} \addcontentsline{toc}{section}{\numberline{}CHAPTER 1. Introduction} \setcounter{section}{1}

\section{This is a normal section}

\section*{\huge CHAPTER 2.\ A title} \addcontentsline{toc}{section}{\numberline{}CHAPTER 2. A title} \setcounter{section}{1}

\section{This is a normal section}

\end{document}

enter image description here

Of course you want to override the silly decision to set the section counter to 1 and maybe also to modify what goes in the table of contents.

\documentclass{article}

\NewCommandCopy{\latexsection}{\section}

\RenewDocumentCommand{\section}{sO{#3}m}{% \IfBooleanTF{#1}{\fakechapter{#3}}{\latexsection[#2]{#3}}% }

\newcommand{\fakechapter}[1]{\makefakechapter#1\makefakechapter} \def\makefakechapter #1CHAPTER #2.#3\makefakechapter{% \makefakechapteraux{#2}#3\makefakechapter } \def\makefakechapteraux#1#2\#3\makefakechapter{% \latexsection*{{\huge\ignorespaces#1\unskip\ $|$\ }{\Large\ignorespaces #3}}% \addcontentsline{toc}{section}{\protect\numberline{}CHAPTER #1. #3}% \removesillyparts } \long\def\removesillyparts#1\setcounter{\removesillypartsaux} \def\removesillypartsaux#1#2{\setcounter{#1}{0}}

\begin{document}

\section*{\huge CHAPTER 1.\ Introduction} \addcontentsline{toc}{section}{\numberline{}CHAPTER 1. Introduction} \setcounter{section}{1}

\section{This is a normal section}

\section*{\huge CHAPTER 2.\ A title} \addcontentsline{toc}{section}{\numberline{}CHAPTER 2. A title} \setcounter{section}{1}

\section{This is a normal section}

\end{document}

enter image description here

egreg
  • 1,121,712