# How to adjust default horizontal and vertical space such that it meets following requirements (related to \parshape, custom list and picture spacing)?

The requirements:

1. Height of pages in the document should not be constrained by constant length (although page width should be).
2. Every line in a paragraph should be indented more than the previous line of that paragraph (by constant length), with first line of each paragraph having no indent at all (indent of each successive line is incremented by constant length).

OUTPUT APPROXIMATION: 1 2 3 123456789012345678901234567890 <<< PARAGRAPH WIDTH This is sentence 1. This is sentence 2. This is sentence 3. This is sentence 4.\par This is sentence 1. This is sentence 2. This is sentence 3. This is sentence 4.

1. Picture placed inside of such paragraph below any line of the paragraph should be indented as if it (the picture) were the next line. However, a picture following another picture in the paragraph should be indented exactly as that picture (which it follows). First line that follows a picture in the paragraph should have same indent as that picture.

OUTPUT APPROXIMATION: 1 2 3 123456789012345678901234567890 <<< PARAGRAPH WIDTH This is sentence 1. This is sentence 2. This is sentence 3. This is sentence 4. | O O | |\ ^ /| <<< PICTURE (INDENTED AS A LINE OF PARAGRAPH) | --- | _____ /O ^ O\ | --- | <<< PICTURE (SAME INDENT AS PREVIOUS PICTURE) \_____/ This is sentence 5. This is sentence 6.

1. Lists placed inside of paragraphs described above should have their items themselves behave like those paragraphs with exception that the first line of an item instead of starting at no indent (like first line of a paragraph does) should start at greater indent than the line preceding the item (unless that line belongs to another item of same list, in which case both items are indented the same).

OUTPUT APPROXIMATION: 1 2 3 123456789012345678901234567890 <<< PARAGRAPH WIDTH This is sentence 1. This is sentence 2. This is sentence 3. This is sentence 4: 1. This is item 1. This <<< LIST LABEL IS INDENTED AS is still item 1. A LINE OF PARAGRAPH Item 1 again. POST-LABEL LINE-INDENTS BEHAVE 2. This is item 2. This AS USUAL BUT WITH | is still item 2: LABEL WIDTH ADDED TO EACH | a) This is item <<< SUB-ITEM AFTER ITEM LINE BEHAVES | "a". Still AS IF IT WERE ITEM AFTER | item "a". PARAGRAPH LINE | b) This is item | "b". This is sentence 5. This is sentence 6.

1. Optional borders should be available around each paragraph, list item, or picture (allowing for paragraphs, list items, and pictures to be separated by horizontal lines, as well as allowing for equations contained within pictures to start with a wide vertical line).
2. Vertical spacing between paragraphs, between paragraphs and pictures, or between pictures should be equal to interline spacing of a paragraph (should be constant throughout the document, with one exception below).
3. If one paragraph or picture (solely containing equation or text) is completely (or almost completely) comprised of larger or smaller font size than the previous paragraph or picture, then vertical spacing between them should be split: such that one half of vertical spacing depends on font size of upper paragraph or picture and is contained within it (within frame of that paragraph or picture) while another half of vertical spacing (likewise) depends on font size of lower paragraph or picture and is (likewise) contained within it (each half of such mixed vertical spacing should be contained within its respective paragraph or picture, such that vertical space of larger font remains within confines of a structure where the text of that font is and likewise for smaller font).

OUTPUT APPROXIMATION: large font size (20pt) <<< PARAGRAPH 1 | <<< LARGE FONT-SIZE INTERLINE-SPACING ---------------------- <<< END OF PARAGRAPH-1 FRAME ---------------------- <<< START OF PARAGRAPH-2 FRAME | <<< NORMAL FONT-SIZE INTERLINE-SPACING normal font size (10pt) <<< PARAGRAPH 2

Let's start with a code sample. It has several problems: (1) it's hard to position the border of math-equation node immediately after the border of preceding paragraph-line (or position it interline-space distance away from that line), (2) border of math-equation node overlaps with right-side bound of the page if 25mm (paragraph last-line's remaining length available after indent of 15mm) is specified (so I have no idea what to add to or subtract from 25mm to make the border of the math-equation node touch without overlap the border of the page), (3) border of math-equation node overlaps with bottom-side bound of the page too.

\documentclass[tikz]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\path node[inner sep=0,draw,text width=40mm,anchor=north west]
{ \parbox{\hsize}
{ \parshape 4 0mm 40mm 5mm 35mm 10mm 30mm 15mm 25mm
\fboxrule0.1pt
\fboxsep0pt
\fbox{hello world}\newline
\fbox{hello world}\newline
\fbox{hello world}\newline
% FOLLOWING DIM EXPRESSION IS NOT ENOUGH TO ALIGN THE NODE FRAME EXACTLY BELOW THE \fbox OF THE PARAGRAPH-LINE (NODE FRAME IS SLIGHTLY HIGHER THAN THE \fbox FRAME)
\vspace{\dimexpr-\lineskip-\baselineskip+0.1pt}
\begin{tikzpicture}
% 25mm (WIDTH OF LAST LINE, CONSIDERING INDENT OF 15mm) IS TOO WIDE, SO THE NODE FRAME OVERLAPS WITH THE PAGE FRAME; 24mm IS NOT APPROPRIATE WIDTH TO ALIGN THE NODE FRAME AT DOCUMENT BOUND WITHOUT WITHOUT OVERLAP
\path node[draw,text width=24mm]
{ \parbox{\hsize}
{ \begin{gather*}
2+2=4
\end{gather*}
}
};
\end{tikzpicture}
}
};
\end{tikzpicture}
\end{document}


You can use your own approach and completely modify this code sample so that it's unrecognizable from the original. What I care about is making the conditions described above work.

PROGRESS:

UPDATE: this answer (by egreg) solved how to position math-equation node-frame right below the frame of the \fbox by reducing \newline\vspace{\dimexpr-\lineskip-\baselineskip+0.1pt} to \vspace{-\lineskip}\newline. But generating vertical space between those frames equal to interline-spacing is still unsolved problem. I guess we can use 1ex or something font-size dependent to generate vertical space where we don't have interline-space length available. Let's make it 1ex then: \vspace{\dimexpr-\lineskip+1ex}\newline.

UPDATE: (David Carlisle suggested that) aside from using standalone class with tikz option to crop height of pages, \pdfpageheight can be used.

UPDATE: It's been suggested (by marmot) not to nest tikz pictures directly but to save them in boxes instead (to avoid confusing tikz and, therefore, avoid unexpected behavior; tikz doesn't expect picture environments to be directly nested inside other picture environments, that's my take from the links provided).

UPDATE: Great progress on alignment of node frames has been made. It turns out, when you set inner sep to zero, the text width starts at the middle of frame's left side and extends to the middle of frame's right side. Similarly with text height: text starts at the middle of frame's top side and extends to the middle of frame's bottom side. This creates complexity by requiring inner node's text width to "know" outer node's line width, which can be avoided. (See this answer, by marmot.)

My advice would be to start with the basic requirements and implement those first, add fancier things such as boxing as you go on. Similarly specifying the PDF page height based on the document content is covered by several existing answers on site as noted in comments under the question.

This might be a start just implementing the basic indent and list layout as far as I understood it.

Note that as no extra box layers are introduced, there are no issues with \lineskip spacing, TeX's standard line breaking ensures even line spacing throughout.

\documentclass{article}
\setlength\textheight{2\textheight}
\pdfpageheight2\paperheight
\newdimen\zzindent
\setlength\parindent{0pt}

\makeatletter

\setlength\zzindent{5mm}
\def\setpshape#1{\zpshape{0}{#1}{\the\dimexpr\linewidth-#1\relax}{}}
\def\zpshape#1#2#3#4{%
\ifdim#2<\linewidth
\edef\zztmp{%
\noexpand\zpshape
{\the\numexpr#1+1\relax}%
{\the\dimexpr#2+\zzindent}%
{\the\dimexpr#3-\zzindent}%
{#4 #2 #3}}\zztmp
\else
\xdef\zcurrentshape{#1 #4 }%
\parshape#1 #4 %
\fi}

\def\enumerate{%
\ifhmode
\par
\endgroup
\let\endenumerate\hendenumerate
\everypar\expandafter{\expandafter\setpshape\expandafter{\the\dimexpr\prevgraf\zzindent\relax}}%
\else
\let\endenumerate\vendenumerate
\fi
\def\item{\par\refstepcounter{enumi}\theenumi. }%
}

\def\hendenumerate{\par
\def\par{\endgraf\everypar{\setpshape{0pt}}\let\par\endgraf}%
\ignorespacesafterend\begingroup\def\@currenvir{enumerate}}

\def\vendenumerate{\par}

\begin{document}

\everypar{\setpshape{0pt}}

one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five

one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five

one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five
\begin{enumerate}% in para list
\item aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa
bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc
bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc
bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc
\item aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa
bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc
bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc
bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc
bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc
bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc
\end{enumerate}
% text after in para list
one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five

zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz
zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz zzz
zzz zzz zzz zzz zzz zzz zzz zzz

\begin{enumerate}% par list starts in vmode
\item aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa
bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc
bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc
bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc
\item aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa
bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc
bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc
bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc
bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc
bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc aaa bbb ccc
\end{enumerate}
% text after  par list
one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five
one two three four five one two three four five one two three four five

\end{document}

• @bp2017 what is the document that needs the weird layout (do not feel you have to answer, but I felt I had to ask:-) – David Carlisle Jun 12 at 19:07
• @bp2017 actually the first two lines are indented by the 5mm increment but I included the item number in the line, so with single digits it gives that appearance, it is easy to set the item number separately and so adjust the parshape logic if that's what you need. – David Carlisle Jun 12 at 19:11
• @bp2017 you could use expl3 or simply use \makebox[1cm]{\theenumi} or whatever fixed width you want to force a fixed label width, and then adjust the first entry in the parshape to match, it's really a minor detail. – David Carlisle Jun 12 at 22:17
• @bp2017 you asked that before... tex.stackexchange.com/questions/492672/… – David Carlisle Jun 13 at 6:53
• @bp2017 exactly same issue as in the question I linked to, there you asked why you got multiple values of a variable at the end of a loop, here I'm looping building up \parshape so I need to evaluate the value at each stage. compare \edef\z{\dimen0} \edef\z{\z \dimen0} \show\z with \edef\z{\the\dimen0} \edef\z{\z \the\dimen0} \show\z – David Carlisle Jun 13 at 15:20

How about avoiding to nest tikzpictures?

\documentclass[tikz]{standalone}

\usepackage{amsmath}
\usepackage{tikz}

\newlength\innerPicLineWidth
\setlength\innerPicLineWidth{0.8pt}
\newlength\outerPicLineWidth
\setlength\outerPicLineWidth{1.5pt}

% SAVE INNER PICTURE IN A BOX (INSTEAD OF NESTING DIRECTLY;
%  DIRECT NESTING HAS COMPLICATIONS)
\newsavebox\picbox
\sbox\picbox
{% MAKE SURE NO SPACE IS PRESENT HERE (OTHERWISE FRAMES OVERLAP)
\begin{tikzpicture}
\path node
[ draw=red,
%
% IF inner sep IS HALF THE line width, NODE CONTENTS
%  DON'T OVERLAP WITH THE FRAME AND NODE WIDTH IS
%   (line width * 2 + text width), WHICH IS INTUITIVE
%
% IF inner sep IS ZERO, CONTENTS OF A NODE OVERLAP WITH
%  ITS FRAME AND NODE WIDTH IS (line width + text width)
%
% THIS HAS IMPLICATIONS: IF OUTER AND INNER NODES BOTH
%  HAVE inner sep SET TO ZERO, STRETCHING INNER NODE TO
%   FILL OUTER NODE WITHOUT OVERLAPPING WITH OUTER
%    NODE'S FRAME WOULD REQUIRE INNER NODE'S text width
%     TO "KNOW" OUTER NODE'S line width (BUT THIS CAN BE
%      AVOIDED BY SETTING inner sep OF EACH NODE TO HALF
%       OF ITS line width)
%
inner sep=\innerPicLineWidth/2,
line width=\innerPicLineWidth,
text width=25mm-\innerPicLineWidth*2
]
{ \parbox{\hsize}
{ \begin{gather*}
2+2=4
\end{gather*}
}
};
\end{tikzpicture}
}

\begin{document}
\begin{tikzpicture}
\path node
[ draw=blue,
inner sep=\outerPicLineWidth/2,
line width=\outerPicLineWidth,
text width=40mm
]
{ \parbox{\hsize}
{ \parshape 4 0mm 40mm 5mm 35mm 10mm 30mm 15mm 25mm
\fboxrule0.1pt
\fboxsep0pt
\fbox{hello world}\newline
\fbox{hello world}\newline
\fbox{hello world}
% GENERATE 1ex SPACE INSTEAD OF \lineskip SPACE
%  BETWEEN INNER NODE FRAME AND \fbox FRAME
\vspace{\dimexpr-\lineskip+1ex}\newline
\usebox\picbox
}
};
\end{tikzpicture}
\end{document}


• @bp2017 Are you looking for \path node[draw=red,text width=25mm-\pgflinewidth,outer sep=0pt,inner sep=0pt] for the first node that goes to the \savebox? Notice that there several dimensions in tikzpictures, such as the inner and outer seps (which are really inner xsep, inner ysep and the same for outer). And of course the line has a width which is symmetric around the boundary path. And note that this is the reason why you should never nest tikzpictures: the pgf keys of the outer will get applied to the inner one. – marmot Jun 11 at 0:18
• @bp2017 I do not know what the aim is but how about text width=25mm-2*\pgflinewidth? – marmot Jun 11 at 0:37
• @bp2017 Sounds like a fair compromise. ;-) – marmot Jun 11 at 0:44
• @bp2017 That is really up to you (but beware of a particular user who always claims making the codes of others "more concise" ;-) – marmot Jun 11 at 0:46
• @bp2017 Not at all. – marmot Jun 12 at 18:02