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

Allow usage of inside/outside #3

Open
beingalink opened this issue Sep 13, 2023 · 11 comments
Open

Allow usage of inside/outside #3

beingalink opened this issue Sep 13, 2023 · 11 comments

Comments

@beingalink
Copy link

Typst now allows defining margins for inside/outside for bound documents. Would be nice if I could use this package like this, too.

Also, making it possible to define different text alignments depending on if the outside margin is on a left or a right side of a page.

Thanks for this very useful package!

@ntjess
Copy link
Owner

ntjess commented Sep 13, 2023

I am not yet inclined to support this by default since it would greatly increase some of the complexity, but does this work for you?

#import "@preview/drafting:0.1.1": *

#import "utils.typ": *

#let (in-margin, out-margin) = (3in, 0.8in)
#let my-note(..args) = {
  locate(loc => {
    let (l, r) = (out-margin, in-margin)
    if calc.odd(counter(page).at(loc).last()) {
      (l, r) = (r, l)
    }
    margin-note(margin-left: l, margin-right: r, ..args)
  })
}

#set page(
  margin: (inside: in-margin, outside: out-margin),
  paper: "us-letter",
  height: auto,
)
#set-page-properties()

#lorem(50)
#my-note[A right note]
#my-note(side: left)[A left note]
#lorem(50)
#pagebreak()
#lorem(50)
#my-note[A right note]
#my-note(side: left)[A left note]
#lorem(50)

image

@beingalink
Copy link
Author

Thank you! That works. I modified the function to automatically do what I want:

#let (in-margin, out-margin) = (3in, 0.8in)
#let my-note(..args) = {
  locate(loc => {
    let (l, r) = (out-margin, in-margin)
    if calc.odd(counter(page).at(loc).last()) {
      (l, r) = (r, l)
      margin-note(margin-left: l, margin-right: r, side: left, ..args)
    } else {
      margin-note(margin-left: l, margin-right: r, side: right, ..args)
    }
  })
}

Would be nice if something like this could be included in the package, but of course it is your decision if the use case fits your conception.

@ntjess
Copy link
Owner

ntjess commented Sep 15, 2023

The reason I'm hesitant is it almost doubles the number of parameters the user must pass to set-page-properties to get things working properly (inside/outside change meanings depending on whether text is ltr or rtl, I'm not sure if odd/even pages will guarantee which margin to use, etc.). I think once #2 is possible (waiting on fundamental typst improvements), this becomes much more doable

@ntjess
Copy link
Owner

ntjess commented Sep 15, 2023

I am inspired by your solution to set the default side to auto, and using whichever margin is larger when unspecified. Then, you don't have to explicitly swap sides when the margin changes

@beingalink
Copy link
Author

I am inspired by your solution to set the default side to auto, and using whichever margin is larger when unspecified. Then, you don't have to explicitly swap sides when the margin changes

That would already help tremendously and actually be as well of a solution! :) 👍

@fredguth
Copy link

fredguth commented Oct 4, 2023

It was not working for me, so I sent a PR with the page oddity check.

@mareikep
Copy link

mareikep commented Dec 7, 2023

#let (in-margin, out-margin) = (3in, 0.8in)
#let my-note(..args) = {
locate(loc => {
let (l, r) = (out-margin, in-margin)
if calc.odd(counter(page).at(loc).last()) {
(l, r) = (r, l)
margin-note(margin-left: l, margin-right: r, side: left, ..args)
} else {
margin-note(margin-left: l, margin-right: r, side: right, ..args)
}
})
}

I came across this when I ran into compiler warnings trying to solve the same issue (switching left/right with different margins depending on odd/even pages). I had found a similar solution and it seemed to work (visually, it produced the desired output), but it throws a warning, that the layout did not converge within 5 attempts. So I tried this solution, which is very similar to mine, and I am getting the very same warning. Has anyone experienced this as well?

@ntjess
Copy link
Owner

ntjess commented Dec 7, 2023

@mareikep thanks for reporting, are you able to share the full source code for your document that generates warnings? It will be much easier for me to identify the cause in that case.

Generally, the only time compiler warnings are unavoidable is when you have >= 4 overlapping notes

@mareikep
Copy link

mareikep commented Dec 8, 2023

@mareikep thanks for reporting, are you able to share the full source code for your document that generates warnings? It will be much easier for me to identify the cause in that case.

Generally, the only time compiler warnings are unavoidable is when you have >= 4 overlapping notes

Sure, I decided to just create a link to share the source in the Typst app, so it's easiest to reproduce.
In this example, commenting out the last margin-note will remove the warning. I have played around with it a lot, depending on how much text is in between two margin-notes and whether it's the left page or not, but haven't figured out yet, what exactly seems to cause the warning.

Hope, you have some fresh ideas :)

@ntjess
Copy link
Owner

ntjess commented Dec 10, 2023

@mareikep the problem is you have locate nested 3 times: hint calls locate, margin-note calls locate (internally), and sticky-hint calls locate. This is what prevents the function from converging. It is instead better to allow your subfunctions to accept loc as an additional argument. I created an additional file called fixed.typ in your code that directly calls margin-note so there is only one nesting of locate and the warning goes away.

@mareikep
Copy link

@mareikep the problem is you have locate nested 3 times: hint calls locate, margin-note calls locate (internally), and sticky-hint calls locate. This is what prevents the function from converging. It is instead better to allow your subfunctions to accept loc as an additional argument. I created an additional file called fixed.typ in your code that directly calls margin-note so there is only one nesting of locate and the warning goes away.

Thanks for the hint, unfortunately the drafting package does not pass theloc to the subfunctions, so I had to update the drafting.typ file manually and then import it locally. Works now and - so far - no warnings or errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants