3

I would like to use commands without having curly braces {} as delimiters. Mostly I would like that the commands work with a line-break as the delimiter.

Herewith a simple example, which does not work (My is displayed but missing the line)

\documentclass[12pt]{report}
\def\chap #1 {\chapter{#1}}
\def\sec #1 {\section{#1}}
\def\subsec #1 {\subsection{#1}}
\def\li #1 {\begin{itemize}\item #1\end{itemize}}
\begin{document}
\chap My First Chapter
\sec My First Section
\subsec My First Section
\li lkdsjfaljsfad
\li lkdsjfaljsfad
\li lkdsjfaljsfad
\end{document}

Would you know how to make it work?

Mico
  • 506,678
  • Welcome to TeX.SE. You are aware about the role of {} in macro arguments? ;-) –  Jun 25 '17 at 11:57
  • 4
    this just breaks all conventions of latex syntax, you really should not do it:-) but apart from the syntax don't you want a single list of three items, not three one-item lists? – David Carlisle Jun 25 '17 at 12:10
  • not much. Hopefully someone may help me. the above example almost works... but missed the >=2 words until the end of the line (LF) – debian82933 Jun 25 '17 at 12:11
  • Not sure what your actual goal is, but maybe the markdown package could be interesting for you – samcarter_is_at_topanswers.xyz Jun 25 '17 at 12:33
  • 1
    @debian82933 Usually newbies ask complicated questions for issues, which are easy to solve. So what is your goal? – Keks Dose Jun 25 '17 at 12:38
  • the format plain define \outer\def\beginsection#1\par and a blanck line is used as delimiter – touhami Jun 25 '17 at 12:44
  • Related: https://tex.stackexchange.com/questions/191267/command-delimited-by-return-or-blank-line – Manuel Jun 25 '17 at 14:12

2 Answers2

4

Here's a LuaLaTeX-based solution. It sets up a Lua function that operates at a very early stage of processing: it scans all lines of input and simply replaces instances of \chap ..... with \chapter{.....}. (In Lua's string.gsub function, the $ character serves as the end-of-line marker.) Ditto for \sec ..., \subsec ..., and \li .... With this setup, LaTeX only gets to "see" the standard macros with standard argument formation; no need to define macros called \chap, \sec, etc.

enter image description here

% !TeX program = lualatex
\documentclass[12pt]{report}
\usepackage{luacode}
\begin{luacode}
function scan_ahead ( s )
  s = string.gsub ( s , "\\chap%s+(.*)$" , "\\chapter{%1}" )
  s = string.gsub ( s , "\\sec%s+(.*)$" , "\\section{%1}" )
  s = string.gsub ( s , "\\subsec%s+(.*)$" , "\\subsection{%1}" )
  s = string.gsub ( s , "\\li%s+(.*)$" , "\\begin{itemize}\\item %1\\end{itemize}")
  return s
end
luatexbase.add_to_callback ( "process_input_buffer",  scan_ahead , "scan_ahead" )
\end{luacode}

\begin{document}
\chap My First Chapter
\sec My First Section
\li lkdsjfaljsfad
\li abc def ghi
\subsec My First Section
\li jkl mno pqr stu
\end{document}
Mico
  • 506,678
3

I don't recommend this -- it does not look well and breaks the tradition of LaTeX commands.

The code is taken from egreg's answer at Using end-of-line delimiter in plain Tex macro (upvoted of course ;-))

There's actually nothing to gain from this and distracts from (proof)reading codes.

Especially \li is not really useful short version \item anyway.

The endline character has usually the catcode 5, so this is converted to a space or \par token (depending on the state) first before further processing the input. In order to make TeX recognize the end of line the catcode has to be changed, to 12 ('other'), in a group, so use

\def\chap{\begingroup\catcode`\^^M=12 \xchap}
{\catcode`\^^M=12 %
 \gdef\xchap#1^^M{\chapter{#1}\endgroup}%
}

to read the content until the end of the line and let the temporary macro \xchap to the display of \chapter{...} etc. (the same for \sec and \subsec.)

Of course, such approaches will break \chap[foo]{foo bar}, i.e. the short version of the chapter title meant for ToC and page headings are not available any longer.

\documentclass[12pt]{report}


\def\chap{\begingroup\catcode`\^^M=12 \xchap}
{\catcode`\^^M=12 %
 \gdef\xchap#1^^M{\chapter{#1}\endgroup}%
}


\def\sec{\begingroup\catcode`\^^M=12 \xsec}
{\catcode`\^^M=12 %
 \gdef\xsec#1^^M{\section{#1}\endgroup}%
}

\def\subsec{\begingroup\catcode`\^^M=12 \xsubsec}
{\catcode`\^^M=12 %
 \gdef\xsubsec#1^^M{\subsection{#1}\endgroup}%
}



\begin{document}
\chap My First Chapter
\sec My First Section
\subsec My First Section
\begin{itemize}
\item lkdsjfaljsfad
\item lkdsjfaljsfad
\item lkdsjfaljsfad
\end{itemize}
\end{document}

However, this is code golfing, in my point of view.

  • the advantage using simple pdflatex is to avoid { } and to make documents clearer and much simpler to type. It is cool and great with \li, \sec, \subsec and it can be simpler to learn. – debian82933 Jun 25 '17 at 12:55
  • 1
    @debian82933: No, it isn't. It's pure laziness. In the long end it will bite you –  Jun 25 '17 at 12:56
  • Lazyness, but rather in first approach for higher efficiency, rapidity for typing long documents. - why actually? Commands with line-break delimited is a good, it is like the first discussion from ritchie about // comment delimited by line-break. The use of // with Line-break is still there today in C. nothing wrong. – debian82933 Jun 25 '17 at 13:01
  • @debian82933: \chapter{Foo Foobar} will work for any document class that provides a chapter command, \chap Foo Foobar only for documents that have the 'abusive' \chap macro -- you have to provide it either with the document itself (blasting up the document size), or write a new class and/or package, let alone that \chap[foo] Foo bar won't work any longer –  Jun 25 '17 at 13:05
  • 1
    @debian82933: comparison of C and LaTeX lead to the wrong end... –  Jun 25 '17 at 13:07
  • Ok, I am fine, it can be tried. I can try to adapt to LaTeX default standard. I want too really. - But, then, how then can you make LaTeX easily readable and simple to type without having all the time brackets everywhere...? the example of \section{my chapter} is very complex to write. – debian82933 Jun 25 '17 at 13:11
  • 1
    @debian82933: I'm using LaTeX for 20 years now and never complained about using those braces. Just because the syntax of macros is changeable to some extent this does not mean that it is advisable to do so. –  Jun 25 '17 at 13:15
  • I can understand that you got used to. Wouldn't it be simpler to use \section blabla and give a linebreak. It would be much simpler to type. altgr + { } is so much keys and long. Furthermore, same story for {\bfseries blabla}, difficult to just bold something, no? – debian82933 Jun 25 '17 at 13:25
  • 1
    Use a US - keyboard layout and you don't need ALTGR ;-) –  Jun 25 '17 at 13:28
  • Great ;) I have a German keyboard, and I can't miss the regular öäü ;) What about a sort of markup language that would do the job of \sec my-section-name,... ? – debian82933 Jun 25 '17 at 13:31
  • 1
    @debian82933: Since you're using Debian you may use a keyboard layout switching command as well -- I have a regular German hardware keyboard, but most times in US software layout to type LaTeX. –  Jun 25 '17 at 13:33
  • Maybe because Latex over-use the braces, which can make it complex to use. However I agree with you that a line-break shall not be the end of a command, since it is useful to break a long line. In C language, we can do so, which is extremely readable. However, then, how to stop a new command input ? – debian82933 Jun 25 '17 at 13:35
  • 1
    @debian82933 - You really ought to reconsider your basic world view, which is (apparently) that "LaTeX over-uses braces". Each programming language has its syntactical requirements; just because you appear to be rather familiar with C doesn't mean that all other programming languages (including TeX) must look behave like C. – Mico Jun 25 '17 at 14:13
  • @debian82933 Regarding the typing bit, most LaTeX editors will have some form of autocompletion, which can make it much faster to write code. In TeXworks for example, you can type s and then hit the Tab-key, and you'll get \section{} with the cursor between the braces. – Torbjørn T. Jun 25 '17 at 19:54
  • @Christian Hupfer (and maybe others): herewith you can find a good workaround for german native speakers, which haven't any easy keyboard. This markup format is a nice workaround to bring an easy typing and it is fairly simple to use, and it is realiable in the time. https://github.com/spartrekus/Unimark – debian82933 Jun 25 '17 at 20:32
  • @debian82933: That's nothing relevant to my answer however -- I don't use markdown ;-) Add the comment to your question rather –  Jun 25 '17 at 20:34