Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to format dtx files? #513

Closed
Cube707 opened this issue Jan 30, 2024 · 15 comments
Closed

how to format dtx files? #513

Cube707 opened this issue Jan 30, 2024 · 15 comments
Labels
answered question has been answered dtx question user-centred question about behaviour of latexindent.pl

Comments

@Cube707
Copy link

Cube707 commented Jan 30, 2024

This is a very heavy feature-request, so feel free to tell me it's out of scope 😄
But if this is of interes to you I would be happy to assist in getting this to work.

When creating LaTeX packages or classes the source-code and documentation is usually written in a single file of format dtx.
This file contains two layers of LaTeX code, the source as expected and the documentation inside comments.

I would like to use latexindent to help me keep a consistent format, but I cant really get it to run properly. I would also argue that this is a common requirement in the LaTeX community and it might make sens to create a solution that can be included in the documentation or build directly into the script, if this is of interest to you.

what are dtx files

The filetype mixes package source-code and documentation in a single file. It can be processed using different standard utilities to produce both the class/package file and a pdf documenting it.

Calling the docstrip utility on the file will stip all comments (and therefore the documentation), leaving only the bare source-code.

Calling pdflatex on the file will process it into a pdf with the proceeding % beeing removed, printing the documentation and all source-code being contained inside macrocode blocks.

original code

Note that lines prefixed with % are not commented out but are valid LaTeX-code.

% \iffalse
%<*driver> ^^A ---------- documentation driver -----------------------------------------
\ProvidesFile{class.dtx}
\documentclass{ltxdoc}
\begin{document}
\DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
% ^^A ---------- document body -----------------------------------------
% \title{A \LaTeX\ class}
% \maketitle
%
% \section{Introduction}
% This is just an example.
%
% \section{Class commands}
% You can use the following options:
%
% \begin{description}
% \item[foo] first command with long paragraph
% \item[bar] second command
% \end{description}
%
% \section{Implementation}
%    \begin{macrocode}
%<*class>
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{HsH-standalone}[2024-01-29 my cool class]
%    \end{macrocode}
%
% \subsection{Implement the commands}
%
% \begin{macro}{\foo}
% this is an example macro.
%    \begin{macrocode}
\def\foo{foo}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\bar}
% this is an example macro.
%    \begin{macrocode}
\def\bar{bar}
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
%<*class>
%    \end{macrocode}
\endinput

actual/given output

running latexindent on the file only formats the not-commented parts (as is expected)

desired output

something similar to this:

% \iffalse
%<*driver> ^^A ---------- documentation driver -----------------------------------------
\ProvidesFile{class.dtx}
\documentclass{ltxdoc}
\begin{document}
\DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
% ^^A ---------- document body -----------------------------------------
%
%\title{A \LaTeX\ class}
%\maketitle
%
%\section{Introduction}
%   This is just an example.
%
%\section{Class commands}
%  You can use the following options:
%
%  \begin{description}
%    \item[foo] first command with
%          long paragraph
%    \item[bar] second command
%  \end{description}
%
%\section{Implementation}
%  \begin{macrocode}
%<*class>
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{HsH-standalone}[2024-01-29 my cool class]
%  \end{macrocode}
%
%  \subsection{Implement the commands}
%
%    \begin{macro}{\foo}
%    this is an example macro.
%    \begin{macrocode}
\def\foo{
  foo
}
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macro}{\bar}
%    this is an example macro.
%    \begin{macrocode}
\def\bar{
  bar
}
%    \end{macrocode}
%    \end{macro}
%
%    \begin{macrocode}
%<*class>
%    \end{macrocode}
\endinput

anything else

  • firstly I assume this will require two separate passes with different settings. This is absolutely fine, but maybe there is a better option?
  • Note the %<..> lines, which are guard-statements and should not be formatted.
  • the confiuration options provided by latexindent should be keept available as much as possible
  • dtx uses ^^A as a comment character, which could be respected
@cmhughes
Copy link
Owner

Thanks for this.

Using the following settings

cube707.yaml

defaultIndent: "  "

specialBeginEnd:
  #
  # store 
  #     ^^A ....
  # as verbatim
  #
  dtxComments:
    begin: \^\^A
    body: [^\n]*?
    end: ''
    lookForThis: verbatim
  #
  # store 
  #     <.*?>
  # as verbatim
  #
  dtxXML:
    begin: \<
    body: [^>]*?
    end: \>
    lookForThis: verbatim

# 
# polyswitch work
#
modifyLineBreaks:
  mandatoryArguments:
    foo:
      MandArgBodyStartsOnOwnLine: 1       # -1,0,1,2,3,4
      RCuBStartsOnOwnLine: 1              # -1,0,1,2,3,4
    bar:
      MandArgBodyStartsOnOwnLine: 1       # -1,0,1,2,3,4
      RCuBStartsOnOwnLine: 1              # -1,0,1,2,3,4

replacements:
  #
  # BEFORE indentation
  #
  # change
  #
  #   % \iffalse
  #
  # into
  #
  #   \iffalse%TOBEREMOVED
  - 
    substitution: |-
      s/^%(.*?)$/$1%TOBEREMOVED/mg
    when: before
  #
  # AFTER indentation
  #
  # change
  #
  #   \iffalse%TOBEREMOVED
  #
  # back into
  #
  #   % \iffalse
  - 
    substitution: |-
      s/^(.*?)%TOBEREMOVED$/% $1/mg
    when: after
  #
  # change
  #
  #   %     <>
  #
  # into
  #
  #   %<>
  - 
    substitution: |-
      s/^%\h*\</%</mg
    when: after
  #
  # change, for example
  #
  #  %<*class>
  #  	\NeedsTeXFormat{LaTeX2e}
  #  	\ProvidesClass{HsH-standalone}[2024-01-29 my cool class]
  #  % \end{macrocode}
  #
  # into
  #
  #  %<*class>
  #  \NeedsTeXFormat{LaTeX2e}
  #  \ProvidesClass{HsH-standalone}[2024-01-29 my cool class]
  #  % \end{macrocode}
  - 
    substitution: |-
      s/^(%.*?\R)((?:[^%].*?$)+?\R)(%)/
      my $begin =  $1;
      my $middle = $2;
      my $end    = $3;
      # remove leading space from first line of $middle
      $middle =~ s|^(\h*)||s;
      my $leadingSpace = ($1?$1:q());
      $middle =~ s|^$leadingSpace||mg;
      $begin.$middle.$end;/sgmxe
    when: after

and calling

latexindent.pl -r -m -l cube707.yaml myfile.dtx

gives the following

% \iffalse
%<*driver> ^^A ---------- documentation driver -----------------------------------------
\ProvidesFile{class.dtx}
\documentclass{ltxdoc}
\begin{document}
\DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
% ^^A ---------- document body -----------------------------------------
% \title{A \LaTeX\ class}
% \maketitle
% 
% \section{Introduction}
% This is just an example.
% 
% \section{Class commands}
% You can use the following options:
% 
% \begin{description}
%   \item[foo] first command with long paragraph
%   \item[bar] second command
% \end{description}
% 
% \section{Implementation}
% \begin{macrocode}
%<*class>
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{HsH-standalone}[2024-01-29 my cool class]
% \end{macrocode}
% 
% \subsection{Implement the commands}
% 
% \begin{macro}{\foo}
%   this is an example macro.
%   \begin{macrocode}
\def\foo{
  foo
}
%   \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\bar}
%   this is an example macro.
%   \begin{macrocode}
\def\bar{
  bar
}
%   \end{macrocode}
% \end{macro}
% 
% \begin{macrocode}
%<*class>
% \end{macrocode}
\endinput

I hope it as you would like :)

@cmhughes cmhughes added question user-centred question about behaviour of latexindent.pl answered question has been answered dtx labels Jan 30, 2024
cmhughes added a commit that referenced this issue Jan 30, 2024
@Cube707
Copy link
Author

Cube707 commented Jan 31, 2024

Thank you very much for this quick response.

I added the following substitution at the end, as I just learned that \end{macrocode} has a hardcoded requirement to have foure leading spaces -_-

  # the \begin{macrocode} and \end{macrocode} need to be indented by exactly four spaces
  -
    substitution: |-
      s/^% *\\(begin|end)\{macrocode\}/%    \\$1\{macrocode\}/mg
    when: after

@Cube707
Copy link
Author

Cube707 commented Jan 31, 2024

Running you config on the full document has opened some more problems.

The following code illustrates some of them:

% \section{Implementation}
%
% \subsection{Executind default options}
% We have some default options that need executing:
%    \begin{macrocode}
\FamilyExecuteOptions{HsH}{%
%<*!standalone>
  fontsize=11pt,
%<article>  parskip=never+,
%<report>  parskip=half+,
  headheight=2.15\baselineskip,
%</!standalone>
%<*article|report>
  toc=listof,
  toc=bibliography,
%</article|report>
  fontfamily=sans-serif,
  linespacing=single,
  faculty=none,
%<*standalone>
  margin=0.25cm,
  multi=tikzpicture,
  multi=circuitikz,
%</standalone>
}
%    \end{macrocode}

running latexindent -r -m -l cube707.yaml testing.dtx in it results in:

% \section{Implementation}
% 
% \subsection{Executind default options}
% We have some default options that need executing:
% \begin{macrocode}
\FamilyExecuteOptions{HsH}{%
%<*!standalone>
    fontsize=11pt,
%<article>  parskip=never+,
%<report>  parskip=half+,
headheight=2.15\baselineskip,
%</!standalone>
%<*article|report>
toc=listof,
toc=bibliography,
%</article|report>
    fontfamily=sans-serif,
    linespacing=single,
    faculty=none,
%<*standalone>
margin=0.25cm,
multi=tikzpicture,
multi=circuitikz,
%</standalone>
  }
% \end{macrocode}
  • Docstrip guard can also be single-lined, my first thought would be to store this in a comment similar to the TOBEREMOVED part
  • different formatting get applied to line 8 and line 11 wehre I am not sure why
  • in general formatting seems to differ depending on if inside Docstrip guards or not

I start leaning heavily towards splitting the whole thing into two. One run to only format the not-commented lines and one run to format only the commented lines with comment characters removed (and some magic to get Docstrip guards to work). This would allow for more clear and easier separation of settings between the two?

What do you think?

@Cube707
Copy link
Author

Cube707 commented Jan 31, 2024

@cmhughes
Copy link
Owner

cmhughes commented Jan 31, 2024 via email

@Cube707
Copy link
Author

Cube707 commented Jan 31, 2024

And feel free to tell me to stop bothering you with this. Its just that I am quite surprised that I can't find much regarding this problem when pretty much every package author has a dtx file and would benefit from having a convenient way to get it prettyfied

@Cube707
Copy link
Author

Cube707 commented Jan 31, 2024

Please give desired output and I'll try to help.

as running latexindent on this simplified, striped version (with defaultIndent:' '):

\FamilyExecuteOptions{HsH}{%
fontsize=11pt,
parskip=never+,
}

will result in

\FamilyExecuteOptions{HsH}{%
  fontsize=11pt,
  parskip=never+,
}

I would like the output to be

% \section{Implementation}
%
% \subsection{Executind default options}
% We have some default options that need executing:
%    \begin{macrocode}
\FamilyExecuteOptions{HsH}{%
%<*!standalone>
  fontsize=11pt,
%<article>  parskip=never+,
%<report>  parskip=half+,
  headheight=2.15\baselineskip,
%</!standalone>
%<*article|report>
  toc=listof,
  toc=bibliography,
%</article|report>
  fontfamily=sans-serif,
  linespacing=single,
  faculty=none,
%<*standalone>
  margin=0.25cm,
  multi=tikzpicture,
  multi=circuitikz,
%</standalone>
}
%    \end{macrocode}

note that the indentation after %<article> should match the rest of the block, if possible

@cmhughes
Copy link
Owner

cmhughes commented Jan 31, 2024 via email

@Cube707
Copy link
Author

Cube707 commented Jan 31, 2024

yes, processing it through docstrip produces the expected results.

%<*TAG> begins a multiline block, %</TAG> ends it.

%<TAG> is a single line block that is ende by \n

this means that lines 7-12 will not be passed to the standalone ouput, but with line 9 only getting passed to the article output and line 10 only to report. Lines 13-14 are for both article and report, lines 17-19 are unguarded and therfor get passed always and lines 20-24 are only for standalone

@cmhughes
Copy link
Owner

Using the following

defaultIndent: "  "

specialBeginEnd:
  iffalse:
    begin: \\iffalse\R
    end: \\fi
  #
  # store 
  #     ^^A ....
  # as verbatim
  #
  dtxComments:
    begin: \^\^A
    body: [^\n]*?
    end: ''
    lookForThis: verbatim
  #
  # store 
  #     <.*?>
  # as verbatim
  #
  dtxXML:
    begin: \<
    body: [^>]*?
    end: \>
    lookForThis: verbatim

noAdditionalIndent:
  iffalse: 1
  macrocode: 1

# 
# polyswitch work
#
modifyLineBreaks:
  mandatoryArguments:
    foo:
      MandArgBodyStartsOnOwnLine: 1       # -1,0,1,2,3,4
      RCuBStartsOnOwnLine: 1              # -1,0,1,2,3,4
    bar:
      MandArgBodyStartsOnOwnLine: 1       # -1,0,1,2,3,4
      RCuBStartsOnOwnLine: 1              # -1,0,1,2,3,4

replacements:
  #
  # BEFORE indentation
  #
  # change
  #
  #   % \iffalse
  #
  # into
  #
  #   \iffalse%TOBEREMOVED
  - 
    substitution: |-
      s/^%(.*?)$/$1%TOBEREMOVED/mg
    when: before
  #
  # AFTER indentation
  #
  # change
  #
  #   \iffalse%TOBEREMOVED
  #
  # back into
  #
  #   % \iffalse
  - 
    substitution: |-
      s/^(.*?)%TOBEREMOVED$/% $1/mg
    when: after
  #
  # change
  #
  #   %     <>
  #
  # into
  #
  #   %<>
  - 
    substitution: |-
      s/^%\h*\</%</mg
    when: after
  #
  # change, for example
  #
  #  %<*class>
  #  	\NeedsTeXFormat{LaTeX2e}
  #  	\ProvidesClass{HsH-standalone}[2024-01-29 my cool class]
  #  % \end{macrocode}
  #
  # into
  #
  #  %<*class>
  #  \NeedsTeXFormat{LaTeX2e}
  #  \ProvidesClass{HsH-standalone}[2024-01-29 my cool class]
  #  % \end{macrocode}
  - 
    substitution: |-
      s@^                         # line begins with
        (%                        # %
          (?!                     # not followed by
            (?:                   # 
              (?:<[*a-zA-Z])      #   <[*a-zA-Z]
              |                   # OR
              (?:<\/[a-zA-Z]+\|)  #   </[a-zA-Z]|
            )                     # 
          )[^\n]*?\R              # anything except a new line
        )                         # 
        (                         # 
          (?:[^%].*?$)+?\R        # %
        )                         # 
        (%)@
      my $begin =  $1;
      my $middle = $2;
      my $end    = $3;
      # remove leading space from first line of $middle
      $middle =~ s|^(\h*)||s;
      my $leadingSpace = ($1?$1:q());
      $middle =~ s|^$leadingSpace||mg;
      $begin.$middle.$end;@sgmxe
    when: after

gives

% \section{Implementation}
% 
% \subsection{Executind default options}
% We have some default options that need executing:
% \begin{macrocode}
\FamilyExecuteOptions{HsH}{%
%<*!standalone>
  fontsize=11pt,
%<article>  parskip=never+,
%<report>  parskip=half+,
  headheight=2.15\baselineskip,
%</!standalone>
%<*article|report>
  toc=listof,
  toc=bibliography,
%</article|report>
  fontfamily=sans-serif,
  linespacing=single,
  faculty=none,
%<*standalone>
  margin=0.25cm,
  multi=tikzpicture,
  multi=circuitikz,
%</standalone>
}
% \end{macrocode}

cmhughes added a commit that referenced this issue Jan 31, 2024
@Cube707
Copy link
Author

Cube707 commented Jan 31, 2024

thank you very much for this effort.

I tried to incorporate all this into one complete version, but couldn't it was to complicated for me.

However using your examples as inspiration I was able to get it mostly working in two separate passes. Using th two settings files dtxDoc.yaml and dtxSource.yaml

defaultIndent: "  "

indentRules:
  section: "  "
  subsection: "  "
indentAfterHeadings:
  section:
    indentAfterThisHeading: 1
    level: 1
  subsection:
    indentAfterThisHeading: 1
    level: 2

indentAfterItems:
  description: 1

specialBeginEnd:
  # ^^A .... as verbatim
  dtxComments:
    begin: \^\^A
    end: \n
    lookForThis: verbatim

  # %<.*?> as verbatim
  dostripMultilineGuards:
    begin: '%\<[*/]'
    end: '\>'
    lookForThis: verbatim

  # \iffalse .. \fi as verbatime
  iffalse:
    begin: \\iffalse
    end: \\fi
    lookForThis: verbatim

  # everything inside macrocode is verbatime,
  # gets formatted by the second pass
  macrocode:
    begin: \\begin\{macrocode\}
    end: \\end\{macrocode\}
    lookForThis: verbatim

  # the custom TBR marker shouldn't be touched
  toBeRemoved:
    begin: '%TBR'
    end: \n
    lookForThis: verbatim

replacements:
  # remove leading %
  - substitution: |-
      s/^%(?!<) ?(.*?)$/$1%TBR/mg
    when: before

  # put leading % back
  - substitution: |-
      s/^(.*?)% *?TBR$/% $1/mg
    when: after

  # the \begin{macrocode} and \end{macrocode} need to be indented by exactly four spaces
  - substitution: |-
      s/^% *\\(begin|end)\{macrocode\}/%    \\$1\{macrocode\}/mg
    when: after
defaultIndent: "  "

specialBeginEnd:
  # skip each line starting with %
  dxtDocLines:
    begin: ^%
    end: \n
    lookForThis: verbatim

  # the custom TBR marker shouldn't be touched
  ToBeRemoved:
    begin: '%TBR'
    end: \n
    lookForThis: verbatim

replacements:
  # move single-line guard to the end of the line
  - substitution: |-
      s/%(\<[^*\/]*?\>)(.*?)$/$2%TBR$1/mg
    when: before

  # put leading guard back
  - substitution: |-
      s/^(.*?)%TBR(.*?)$/%$2$1/mg
    when: after

  # remove leading space from all lines starting with %
  - substitution: |-
      s/^\h*%/%/mg
    when: after

This sucessfully formats the following testing.dxt by calling latexindent -r -l dtxDoc.yaml testing.dtx -w -s && latexindent -r -l dtxSource.yaml testing.dtx -w -s

% \iffalse
%<*driver> ^^A ---------- documentation driver --------------------------------
\ProvidesFile{class.dtx}
\documentclass{ltxdoc}
\begin{document}
\DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
% ^^A ---------- document body ------------------------------------------------
%\title{A \LaTeX\ class}
%  \maketitle
%
%     \section{Introduction}
%  This is just an example.
%
% \section{Class commands}
% You can use the following options:
%
% \begin{description}
% \item[foo] first command with long paragraph tha should be automatically brocken into multiple lines
% \item[bar] second command
%            with two destict lines
% \end{description}
%
% \section{Implementation}
%
%           Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec volutpat turpis sit amet dapibus facilisis. Duis mauris mi, scelerisque et.
%
%    \begin{macrocode}
%<*class>
 \NeedsTeXFormat{LaTeX2e}
         \ProvidesClass{HsH-standalone}[2024-01-29 my cool class]
%    \end{macrocode}
%
%    \subsection{Executind default options}
%    We have some default options that need executing:
%    \begin{macrocode}
\FamilyExecuteOptions{HsH}{%
%<*!standalone>
fontsize=11pt,
%<article>    parskip=never+,
%<report> parskip=half+,
  headheight=2.15\baselineskip,
%</!standalone>
%<*article|report>
toc=listof,
     toc=bibliography,
%</article|report>
  fontfamily=sans-serif,
 linespacing=single,
  faculty=none,
%<*standalone>
margin=0.25cm,
multi=tikzpicture,
multi=circuitikz,
%</standalone>
}
%    \end{macrocode}
%
% \subsection{Implement the commands}
%
%    \begin{macro}{\foo}
% this is an example macro.
%    \begin{macrocode}
   \def\foo{foo}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\bar}
% this is an example macro.
%    \begin{macrocode}
\def\bar{bar}
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
%<*class>
%    \end{macrocode}
\endinput

@Cube707
Copy link
Author

Cube707 commented Jan 31, 2024

however I have some question:

is there really no better way to mark a line as "don't put any indentation"? Do I really have to pull the guards %<tag> back to the beginning via a replacement?

Do you see any way to get multiline support -m working? The \item and the Lorem ipsum lines could benefit from beeing split and this would work without the %, but fails with my replacement "hack". Any Ideas for this ?

@cmhughes
Copy link
Owner

cmhughes commented Feb 4, 2024

If I used the following settings

defaultIndent: "  "

specialBeginEnd:
  iffalse:
    begin: \\iffalse\R
    end: \\fi
  #
  # store 
  #     ^^A ....
  # as verbatim
  #
  dtxComments:
    begin: \^\^A
    body: [^\n]*?
    end: ''
    lookForThis: verbatim
  #
  # store 
  #     <.*?>
  # as verbatim
  #
  dtxXML:
    begin: \<
    body: [^>]*?
    end: \>
    lookForThis: verbatim

noAdditionalIndent:
  iffalse: 1
  macrocode: 1

# 
# polyswitch work
#
modifyLineBreaks:
  mandatoryArguments:
    foo:
      MandArgBodyStartsOnOwnLine: 1       # -1,0,1,2,3,4
      RCuBStartsOnOwnLine: 1              # -1,0,1,2,3,4
    bar:
      MandArgBodyStartsOnOwnLine: 1       # -1,0,1,2,3,4
      RCuBStartsOnOwnLine: 1              # -1,0,1,2,3,4

replacements:
  #
  # BEFORE indentation
  #
  # change
  #
  #   % \iffalse
  #
  # into
  #
  #   \iffalse%TOBEREMOVED
  - 
    substitution: |-
      s/^%(.*?)$/$1%TOBEREMOVED/mg
    when: before
  #
  # AFTER indentation
  #
  # change
  #
  #   \iffalse%TOBEREMOVED
  #
  # back into
  #
  #   % \iffalse
  - 
    substitution: |-
      s/^(.*?)%TOBEREMOVED$/% $1/mg
    when: after
  #
  # change
  #
  #   %     <>
  #
  # into
  #
  #   %<>
  - 
    substitution: |-
      s/^%\h*\</%</mg
    when: after
  #
  # change, for example
  #
  #  %<*class>
  #  	\NeedsTeXFormat{LaTeX2e}
  #  	\ProvidesClass{HsH-standalone}[2024-01-29 my cool class]
  #  % \end{macrocode}
  #
  # into
  #
  #  %<*class>
  #  \NeedsTeXFormat{LaTeX2e}
  #  \ProvidesClass{HsH-standalone}[2024-01-29 my cool class]
  #  % \end{macrocode}
  - 
    substitution: |-
      s@^                         # line begins with
        (%                        # %
          (?!                     # not followed by
            (?:                   # 
              (?:<[*a-zA-Z])      #   <[*a-zA-Z]
              |                   # OR
              (?:<\/[a-zA-Z]+\|)  #   </[a-zA-Z]|
            )                     # 
          )[^\n]*?\R              # anything except a new line
        )                         # 
        (                         # 
          (?:[^%].*?$)+?\R        # %
        )                         # 
        (%)@
      my $begin =  $1;
      my $middle = $2;
      my $end    = $3;
      # remove leading space from first line of $middle
      $middle =~ s|^(\h*)||s;
      my $leadingSpace = ($1?$1:q());
      $middle =~ s|^$leadingSpace||mg;
      $begin.$middle.$end;@sgmxe
    when: after

on your most recent file then I receive

% \iffalse
%<*driver> ^^A ---------- documentation driver --------------------------------
\ProvidesFile{class.dtx}
\documentclass{ltxdoc}
\begin{document}
\DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
% ^^A ---------- document body ------------------------------------------------
% \title{A \LaTeX\ class}
% \maketitle
% 
% \section{Introduction}
% This is just an example.
% 
% \section{Class commands}
% You can use the following options:
% 
% \begin{description}
%   \item[foo] first command with long paragraph tha should be automatically brocken into multiple lines
%   \item[bar] second command
%         with two destict lines
% \end{description}
% 
% \section{Implementation}
% 
% Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec volutpat turpis sit amet dapibus facilisis. Duis mauris mi, scelerisque et.
% 
% \begin{macrocode}
%<*class>
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{HsH-standalone}[2024-01-29 my cool class]
% \end{macrocode}
% 
% \subsection{Executind default options}
% We have some default options that need executing:
% \begin{macrocode}
\FamilyExecuteOptions{HsH}{%
%<*!standalone>
  fontsize=11pt,
%<article>    parskip=never+,
%<report> parskip=half+,
  headheight=2.15\baselineskip,
%</!standalone>
%<*article|report>
  toc=listof,
  toc=bibliography,
%</article|report>
  fontfamily=sans-serif,
  linespacing=single,
  faculty=none,
%<*standalone>
  margin=0.25cm,
  multi=tikzpicture,
  multi=circuitikz,
%</standalone>
}
% \end{macrocode}
% 
% \subsection{Implement the commands}
% 
% \begin{macro}{\foo}
%   this is an example macro.
%   \begin{macrocode}
\def\foo{
  foo
}
%   \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\bar}
%   this is an example macro.
%   \begin{macrocode}
\def\bar{
  bar
}
%   \end{macrocode}
% \end{macro}
% 
% \begin{macrocode}
%<*class>
% \end{macrocode}
\endinput

Is this what you want? If you're wanting to text wrap (?) please do be specific with what you're starting with, and what you'd like to receive.

cmhughes added a commit that referenced this issue Feb 4, 2024
@cmhughes cmhughes closed this as completed Feb 8, 2024
@Cube707
Copy link
Author

Cube707 commented Mar 1, 2024

Sorry for the long delay, I was on holliday.

I think we misunderstood each other somewhat. This request has always been about generall support for .dtx files, not getting a specific use case to work.

I wanted to get a understanding on the following:

  • Can dtx-file formatting be achieved? (we have shown it can be)
  • Can all configuration-options of latexindent be made to work on both the normal source as well as the documentation inside the comments? (we established it is possible and that it is helpful to use two separate config-files and two runs)
  • What limitations apply? (it seems to be that word-wrapping will not work)

And I wanted to know if there is interest to improve on this (I am willing to work on this if you are not intersted in working on that but would accept improvements in this regard). Should this only be in form of documentation and examples or is there interest to improve actual functionality and reduce/remove the limitations.

@cmhughes
Copy link
Owner

cmhughes commented Mar 2, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered question has been answered dtx question user-centred question about behaviour of latexindent.pl
Projects
None yet
Development

No branches or pull requests

2 participants